Makefile 依赖项生成不检查标头
我正在使用一个 makefile 来自动生成依赖项。但通过我的文件,我发现虽然头文件的更改会导致代码重新编译,但它们不会导致依赖项重新生成,正如我认为的那样。谁能看到我错过了什么?
.SUFFIXES : .hpp .cpp .d .o
SOURCES=main.cpp sub1.cpp sub2.cpp
OBJECTS=${SOURCES:.cpp=.o}
DEPENDENCIES=${SOURCES:.cpp=.d}
.cpp.d:
g++ -MM $< > $@
.cpp.o:
g++ $< -c `pkg-config gtkmm-2.4 --cflags --libs` -g
calculator: ${OBJECTS} ${DEPENDENCIES}
g++ ${OBJECTS} -o calculator `pkg-config gtkmm-2.4 --cflags --libs` -g
include ${DEPENDENCIES}
I'm using a makefile intending to generate dependencies automatically. But with my file, I find that although changes to the header files cause the code to be recompiled, they don't cause the dependencies to be regenerated, as I think they ought. Can anyone see what I have missed out?
.SUFFIXES : .hpp .cpp .d .o
SOURCES=main.cpp sub1.cpp sub2.cpp
OBJECTS=${SOURCES:.cpp=.o}
DEPENDENCIES=${SOURCES:.cpp=.d}
.cpp.d:
g++ -MM lt; > $@
.cpp.o:
g++ lt; -c `pkg-config gtkmm-2.4 --cflags --libs` -g
calculator: ${OBJECTS} ${DEPENDENCIES}
g++ ${OBJECTS} -o calculator `pkg-config gtkmm-2.4 --cflags --libs` -g
include ${DEPENDENCIES}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然我同意 Dummy00001 的解决方案,但向 g++ 命令添加 -MP 标志以生成依赖文件可能会有所帮助。它的作用是添加 PHONY 目标以及依赖项列表中所有头文件的名称。
即,如果
g++ -MM $<
生成,则
g++ -MM -MP $<
生成,这将有助于 make 即使重命名完成或文件也不会中断已被删除
While I agree with Dummy00001's solution, it might help to add a -MP flag to g++ command to generate the dependency files. What it does is it adds PHONY targets with the names of all the header files in the dependency list.
i.e. if
g++ -MM $<
generatesthen
g++ -MM -MP $<
generatesthis would help the make not to break even if a renaming is done or a file has been deleted
我自己找到了解决方案。这个技巧也出现在GNU make 官方文档中。
生成依赖项的行应如下所示:
sed 将依赖项行从“main.o: main.cpp include/hello.hpp”转换为“main.o main.d: main.cpp include/hello.hpp” (来自我的最小化测试的示例)从而使 .d 依赖于与 .o 文件本身相同的文件。
虽然我个人建议使用例如 SCons ,它能够自动依赖跟踪,因为(根据我的经验)当引入新的头文件或重命名某些文件时,GNU make 解决方案经常会中断。
Found solution myself. The trick also appears in the official GNU make documentation.
The line to generate the dependencies should look like that:
The sed translates the dependency line from "main.o: main.cpp include/hello.hpp" into "main.o main.d: main.cpp include/hello.hpp" (example from my minimized test) thus making .d depend on the same files as the .o file itself.
Though I personally recommend to use e.g. SCons which is capable of automatic dependency tracking, as (in my experience) the GNU make solution breaks often when a new header file is introduced or some files were renamed.