为C / C ++中的项目生成一个makefile的依赖关系 [英] generate dependencies for a makefile for a project in C/C++

查看:174
本文介绍了为C / C ++中的项目生成一个makefile的依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目有一个具有破坏的依赖项的makefile。有没有最好的方法来生成一个可以在makefile中使用的项目的依赖项列表,而不是用手或用手写的perl脚本检查每个源文件?

解决方案

http://make.paulandlesley.org/autodep .html 有一个很好的解决方案。



绝对。 g ++ -MM将生成一个兼容的GMake兼容列表。我使用这样的东西:

 #添加.d以创建公认的后缀。 
SUFFIXES + = .d

#当我们制作这些目标时,我们不需要清理
NODEPS:= clean标签svn
#查找所有src /目录下的C ++文件
SOURCES:= $(shell find src / -name* .cpp)
#这些依赖文件,它们会在创建后清理
DEPFILES:= $(patsubst%.cpp,%。d,$(SOURCES))

#在清理时不要创建依赖关系,例如
ifeq 0,$($(findstring $(MAKECMDGOALS),$(NODEPS))))
#Chances是,这些文件不存在。 GMake将创建它们,然后
#clean自动以后
-include $(DEPFILES)
endif

#这是创建依赖关系文件的规则
src /%d:src /%cpp
$(CXX)$(CXXFLAGS)-MM -MT'$(patsubst src /%。cpp,obj /%。o,$& < -MF $ @

#此规则执行编译
obj /%。o:src /%。cpp src /%。d src /%。h
@ $ MKDIR)$(dir $ @)
$(CXX)$(CXXFLAGS)-o $ @ -c $<

这样做会自动为已经更改的每个文件生成依赖关系,并根据你有什么规则。这允许我将新文件转储到src /目录中,并将它们自动编译为依赖关系和所有。


I have a project that has a makefile with broken dependencies. Is there any best known way to generate a list of dependencies for the project that I can use in the makefile, other than examining each source file by hand or with a hand written perl script?

解决方案

http://make.paulandlesley.org/autodep.html has a good solution.

Absolutely. g++ -MM will generate a GMake compatible list of dependencies. I use something like this:

# 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 $<

What this will do is automatically generate the dependencies for each file that has changed, and compile them according to whatever rules you have in place. This allows me to just dump new files into the src/ directory, and have them compiled in automatically dependencies and all.

这篇关于为C / C ++中的项目生成一个makefile的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆