makefile学习笔记

来源:互联网 发布:网络问卷有哪些网站 编辑:程序博客网 时间:2024/06/05 17:44

使用自动变量编写makefile 。makefile 有3个非常有用的变量,分别是$@代表目标文件,$^代表所有依赖文件,$<代表第一个依赖文件。

例如:main: main.o test1.o test2.o

                    gcc -o main main.o test1.o test2.o

          main.o: main.c test1.h test2.h

                    gcc -c main.c

         test1.o: test1.c test1.h

                    gcc -c test1.c

         test2.o: test2.c test2.h

                   gcc -c test2.c

        clear:

                  rm -f *.o main


可使用自动变量改写为:


         CC=gcc

         OBJS=main.o test1.o test2.o

         main: $(OBJS)

                   $(CC) -o $@ $^

          main.o: main.c test1.h test2.h

                    $(CC) -c $<

         test1.o: test1.c test1.h

                   $(CC) -c $<

         test2.o: test2.c test2.h

                   $(CC) -c $<

        .PHONY:clear

        clear:

                  rm -f main $(OBJS)