generate dependencies for a makefile for a project in C/C++(为 C/C++ 中的项目的 makefile 生成依赖项)
问题描述
我有一个项目,其生成文件的依赖项已损坏.除了手动检查每个源文件或使用手写的 perl 脚本之外,还有什么最知名的方法可以为我可以在 makefile 中使用的项目生成依赖项列表?
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?
推荐答案
GNU make 的文档提供了一个很好的解决方案.
GNU make's documentation provides a good solution.
当然.g++ -MM <your file>
将生成一个与 GMake 兼容的依赖项列表.我使用这样的东西:
Absolutely. g++ -MM <your file>
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 $<
注意: $(CXX)
/gcc
命令必须是 前面有一个硬标签
这将自动为每个已更改的文件生成依赖项,并根据您现有的任何规则编译它们.这允许我将新文件转储到 src/
目录中,并自动编译它们、依赖项和所有内容.
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.
这篇关于为 C/C++ 中的项目的 makefile 生成依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为 C/C++ 中的项目的 makefile 生成依赖项


基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01