天天看點

Makefile生成多個可執行檔案如何撰寫生成多個可執行檔案的Makefile?

如何撰寫生成多個可執行檔案的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可以通過隐式規則推導出來。

轉載于:https://www.cnblogs.com/dswei/p/4273187.html