Makefile自動產生依賴
在寫makefile時,有一種簡單通用的方法指定目標文件對頭文件的依賴關係。其使用基礎是編譯器能分析源文件中頭文件的包含關係。
比如:gcc -M
選項可以自動產生依賴關係。
$ echo "#incldue <stdio.h>" > stdio.c
$ gcc -M stdio.c
stdio.o: stdio.c /usr/include/stdio.h /usr/include/_ansi.h \...
下面的makefile:
VPATH = src include
CPPFLAGS - -I include
SOURCES = count_words.c lexer.c counter.c
count_words: counter.c lexer.c -lfl
count_words.c:counter.h
counter.c : counter.h lexer.h
lexer.o : lexer.h
include $(subst .c,.d,$(SOURCES))
%.d: %.c
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
make
會把include
指令所指定的文件視爲需要跟新的工作目標。