makefile中自动将头文件添加到依赖中

1 如何将头文件添加到依赖中?

先列出以下几条命令:
gcc -M c.c :打印出依赖,预处理结束就停止编译。
gcc -M -MF c.d c.c:把依赖写入文件c.d,预处理结束就停止编译。
gcc -c -o c.o c.c -MD -MF c.d:编译c.c输出目标文件为c.o, 并把把依赖写入文件c.d。

示例makefile如下:

objs = a.o b.o c.o

dep_files := $(patsubst %,.%.d, $(objs))
dep_files := $(wildcard $(dep_files))

test: $(objs)
	gcc -o test $^

ifneq ($(dep_files),)
include $(dep_files)
endif

%.o : %.c
	gcc -c -o $@ $< -MD -MF .$@.d

clean:
	rm *.o test

distclean:
	rm $(dep_files)
	
.PHONY: clean

猜你喜欢

转载自blog.csdn.net/SlowIsFastLemon/article/details/85013512