如何撰写生成多个可执行文件的Makefile?
例如工程需要生成多个可执行文件:
test1.c; test2.c; test3.c
test1.h; test2.h; test3.h
我们希望生成三个可执行文件:test1, test2, test3
all : test1 test2 test3
.PHONY : all
test1 : test1.o
cc -o test1 test1.o
test2 : test2.o
cc -o test2 test2.o
test3 : test3.o
cc -o test3 test3.o
test1.o : test1.c test1.h
cc -c test1.c
test2.o : test2.c test2.h
cc -c test2.c
test3.o : test3.c test3.h
cc -c test3.c
.PHONY : clean
clean:
rm test1 test2 test3 *.o
第二行说明all是个“伪目标”,make不会生成“all”这个可执行文件,而是执行后面的多个目标。该行可以不写,因为make可以通过隐式规则推导出来。