2012-11-28 87 views

回答

1

我的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 
+0

这是如果你有以上的src makefile的一个目录。我会更改它以将其放入src目录中? –

+0

我只问,因为我不认为我的main.cpp实际上是运行一次,我使所有的文件 –

+0

简单。只需更改所有查找src的东西即可找到。一些东西。例如找到src -name'* .o'来查找。 -name'* .o'。找到的第一个参数是找到cpp或.o文件的位置。 –

1

一个简单的方法就是将所有的源添加到一个变量中并在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) 

这里是一个link到一本好书,开始学习制作。