生成依赖于在C / C项目一个makefile ++ [英] generate dependencies for a makefile for a project in C/C++

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

问题描述

我有了断的依赖生成文件的项目。有没有最有名的方法来生成依赖关系,我可以在makefile使用,除了用手或用手写perl脚本检查每个源文件的项目列表?

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 有一个很好的解决方案。

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

当然可以。 G ++ -MM会产生依赖性的GMAKE兼容列表。我用的是这样的:

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

这将完成自动生成已更改的每个文件的依赖关系,并根据任何编译它们的规则你必须到位。这使我只是转储新文件到src /目录,并将它们编译自动依赖关系和所有。

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天全站免登陆