我有一个项目,其生成文件的依赖项已损坏。除了手动检查每个源文件或使用手写的 perl 脚本之外,是否有任何最知名的方法可以为我可以在 makefile 中使用的项目生成依赖项列表?
Nathan Fellman
问问题
54392 次
5 回答
54
GNU make的文档提供了一个很好的解决方案。
绝对地。g++ -MM <your file>
将生成一个与 GMake 兼容的依赖项列表。我使用这样的东西:
# Add .d to Make's recognized suffixes.
SUFFIXES += .d
#We don't need to clean up when we're making these targets
NODEPS:=clean tags svn
#Find all the C++ files in the src/ directory
SOURCES:=$(shell find src/ -name "*.cpp")
#These are the dependency files, which make will clean up after it creates them
DEPFILES:=$(patsubst %.cpp,%.d,$(SOURCES))
#Don't create dependencies when we're cleaning, for instance
ifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NODEPS))))
#Chances are, these files don't exist. GMake will create them and
#clean up automatically afterwards
-include $(DEPFILES)
endif
#This is the rule for creating the dependency files
src/%.d: src/%.cpp
$(CXX) $(CXXFLAGS) -MM -MT '$(patsubst src/%.cpp,obj/%.o,$<)' $< -MF $@
#This rule does the compilation
obj/%.o: src/%.cpp src/%.d src/%.h
@$(MKDIR) $(dir $@)
$(CXX) $(CXXFLAGS) -o $@ -c $<
注意: $(CXX)
/gcc
命令必须以硬制表符开头
这将自动为每个已更改的文件生成依赖项,并根据您现有的任何规则编译它们。这允许我将新文件转储到src/
目录中,并自动编译它们、依赖项和所有内容。
于 2008-11-24T09:56:04.327 回答
21
现在特别阅读了这部分,我认为有一个更简单的解决方案,只要你有一个合理的最新版本的 gcc/g++。如果您只是添加-MMD
到您的CFLAGS
,定义一个代表所有目标文件的变量OBJS
,然后执行:
-include $(OBJS:%.o=%.d)
那么这应该为您提供一个高效且简单的自动依赖构建系统。
于 2012-04-16T03:29:26.220 回答
7
GNU C 预处理器 cpp 有一个选项 -MM,它根据包含模式生成一组合适的依赖项。
于 2008-11-24T09:52:45.583 回答
5
我只是将它添加到 makefile 中,它工作得很好:
-include Makefile.deps
Makefile.deps:
$(CC) $(CFLAGS) -MM *.[ch] > Makefile.deps
于 2012-11-24T21:04:28.947 回答
0
Digital Mars C/C++ 编译器带有一个makedep工具。
于 2008-11-25T16:28:26.733 回答