为 C/C++ 中的项目生成 makefile 的依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/313778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:34:13  来源:igfitidea点击:

generate dependencies for a makefile for a project in C/C++

c++cmakefiledependencies

提问by Nathan Fellman

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?

我有一个项目,其中的 makefile 依赖项已损坏。除了手动或使用手写 perl 脚本检查每个源文件之外,是否有任何最著名的方法可以为我可以在 makefile 中使用的项目生成依赖项列表?

回答by Stefan Mai

GNU make's documentation provides a good solution.

GNU make的文档提供了一个很好的解决方案。

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

绝对地。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 $<

Note:$(CXX)/gcccommand must be preceded with a hard tab

注意:$(CXX)/gcc命令前面必须有硬制表符

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

这将自动为每个已更改的文件生成依赖项,并根据您现有的任何规则编译它们。这允许我将新文件转储到src/目录中,并自动编译它们,依赖项等等。

回答by Christopher Smith

Having now read this portion in particularI think there is a much easier solution out there, as long as you have a reasonably up to date version of gcc/g++. If you just add -MMDto your CFLAGS, define a variable OBJSrepresenting all your object files, and then do:

现在特别阅读了这一部分,我认为有一个更简单的解决方案,只要你有一个合理的最新版本的 gcc/g++。如果您只是添加-MMD到您的CFLAGS,请定义一个OBJS代表所有目标文件的变量,然后执行以下操作:

-include $(OBJS:%.o=%.d)

then that should get you both an efficient and simple automatic dependency build system.

那么这应该会给你一个高效而简单的自动依赖构建系统。

回答by Barry Kelly

The GNU C preprocessor cpp has an option, -MM, which produces a make-suitable set of dependencies based on inclusion patterns.

GNU C 预处理器 cpp 有一个选项 -MM,它根据包含模式生成一组适合的依赖项。

回答by fferri

I just add this to the makefile and it works nicely:

我只是将它添加到 makefile 中,它运行良好:

-include Makefile.deps

Makefile.deps:
    $(CC) $(CFLAGS) -MM *.[ch] > Makefile.deps

回答by Walter Bright

The Digital Mars C/C++ compiler comes with a makedeptool.

Digital Mars C/C++ 编译器带有makedep工具。