我目前在使用gnu make时遇到问题,在makefile中进行以下设置:(仅重要的部分)
all: bin/game
bin/game: obj/main.o
gcc -o bin/game obj/main.o
obj/main.o: src/main.cpp
gcc -c src/main.cpp -o obj/main.o
# Don't do anything, just make sure that library.hpp hasn't changed
src/main.cpp: include/library.hpp
# Don't do anything, just make sure that part1 and part2 haven't changed
include/library.hpp: include/library/part1.hpp\
include/library/part2.hpp
在这一系列漫长的需求中,尽管在我更新src / main.cpp时(当我更新{{时)它会重建bin/game
,但实际项目中还有更多的目标文件,源文件和头文件。 1}},include/library/part1.hpp
甚至include/library/part2.hpp
,即使它依赖它也不会重建include/library.hpp
。
是因为make本身没有改变而忽略了src / main.cpp的必要条件吗?
我注意到将bin/game
移至include/library.hpp
的必备条件解决了此特定问题,尽管在更改obj/main.o
或include/library/part1.hpp
时它仍然没有更新。
是否有一种方法可以将食谱按照我在此处所做的方式“划分”级别,以使事情更有条理,而不是将它们全部列在目标中?
答案 0 :(得分:1)
src/main.cpp: include/library.hpp
告诉src/main.cpp
更改后,make重建include/library.hpp
。这是不正确的,因为您构建的是obj/main.o
。修复:
obj/main.o : include/library.hpp
接下来,由于您没有构建include/library.hpp
或src/main.cpp
,因此它们的依赖项是无用的。修复:
obj/main.o : include/library.hpp include/library/part1.hpp include/library/part2.hpp
在实践中,您不想自己指定标头依赖项,因为这很繁琐且容易出错。编译器可以为您做到这一点:
obj/main.o: src/main.cpp
gcc -c src/main.cpp -o obj/main.o -MD -MP
-include obj/main.d # produced by -MD -MP
并使用src/main.cpp : ...
和include/library.hpp : ...
删除这些行。
您可以检查obj/main.d
并查看obj/main.o
所依赖的所有文件。
在第一个构建中,您不需要标头依赖项,因为它必须构建所有内容(如果-include obj/main.d
不存在,则obj/main.d
不会失败)。在后续的构建中,它使用由先前的构建生成的依赖项来决定需要重建的内容。