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

发布于 2024-07-09 23:13:06 字数 100 浏览 4 评论 0原文

我有一个项目,其 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遗心遗梦遗幸福 2024-07-16 23:13:06

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

绝对地。 g++ -MM 将生成 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,
lt;)' 
lt; -MF $@

#This rule does the compilation
obj/%.o: src/%.cpp src/%.d src/%.h
    @$(MKDIR) $(dir $@)
    $(CXX) $(CXXFLAGS) -o $@ -c 
lt;

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

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

GNU make's documentation provides a good solution.

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,
lt;)' 
lt; -MF $@

#This rule does the compilation
obj/%.o: src/%.cpp src/%.d src/%.h
    @$(MKDIR) $(dir $@)
    $(CXX) $(CXXFLAGS) -o $@ -c 
lt;

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

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.

榕城若虚 2024-07-16 23:13:06

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

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

那么这应该可以让您既是一个高效又简单的自动依赖关系构建系统。

Having now read this portion in particular I 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 -MMD to your CFLAGS, define a variable OBJS representing all your object files, and then do:

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

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

君勿笑 2024-07-16 23:13:06

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

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

情话已封尘 2024-07-16 23:13:06

我只需将其添加到 makefile 中,它就可以很好地工作:

-include Makefile.deps

Makefile.deps:
    $(CC) $(CFLAGS) -MM *.c > $@

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

-include Makefile.deps

Makefile.deps:
    $(CC) $(CFLAGS) -MM *.c > $@
朱染 2024-07-16 23:13:06

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

The Digital Mars C/C++ compiler comes with a makedep tool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文