簡體   English   中英

如何在子目錄中生成包含源的Makefile?

[英]How to generate a Makefile with sources in sub-directories?

我有以下列方式來源。

/src/main.cpp
/src/hearts/hearts.cpp
/src/spades/spades.cpp
/src/oldmaid/oldmaid.cpp

我該如何為此創建一個makefile?

一種簡單的方法是將所有源添加到變量中,並在make命令中使用該變量。 這是一個相關部分的片段。

APP_SRC=src/main.cpp \
    src/hearts/hearts.cpp \
    src/spades/spades.cpp \
    src/oldmaid/oldmaid.cpp

CC=g++
CFLAGS= -Wall (and any other flags you need)


#
# Rules for building the application and library 
#
all: 
make bin


bin: 
$(CC)  $(CFLAGS) $(APP_SRC)

這里有一本好書的鏈接 ,可以開始學習Make。

我的Makefile的摘錄。 這將搜索src目錄中的cpp文件並進行編譯。 您可以添加新文件並自動選擇它們。

CC = g++  

all: compile
   find src -name '*.o' -print0 | xargs -0 $(CC) -o myExecutable

compile:
    find src -name '*.cpp' -print0 | xargs -0 $(CC) -c

clean:
    find src -iname '*.o' -print0 | xargs -0 rm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM