Nat*_*man 34 c c++ dependencies makefile
我有一个项目,其中包含一个具有破坏依赖项的makefile.是否有任何最着名的方法来生成我可以在makefile中使用的项目的依赖项列表,而不是手动检查每个源文件或手写perl脚本?
Ste*_*Mai 49
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 $<
Run Code Online (Sandbox Code Playgroud)
注意: $(CXX)/gcccommand必须以硬标签开头
这样做会自动为已更改的每个文件生成依赖项,并根据您拥有的任何规则进行编译.这允许我只是将新文件转储到src/目录中,并自动编译它们,依赖项和所有文件.
Chr*_*ith 19
现在已经特别阅读了这一部分,我认为只要你有一个合理的gcc/g ++最新版本,就有更简单的解决方案.如果您只是添加-MMD到您的CFLAGS,请定义一个OBJS表示所有目标文件的变量,然后执行以下操作:
-include $(OBJS:%.o=%.d)
Run Code Online (Sandbox Code Playgroud)
那么这应该会让你既有效又简单的自动依赖构建系统.
我只是将其添加到makefile中,并且效果很好:
-include Makefile.deps
Makefile.deps:
$(CC) $(CFLAGS) -MM *.[ch] > Makefile.deps
Run Code Online (Sandbox Code Playgroud)