在makefile中使用g ++和-MMD来自动生成依赖关系 [英] Using g++ with -MMD in makefile to automatically generate dependencies

查看:1735
本文介绍了在makefile中使用g ++和-MMD来自动生成依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道下面的makefile将使预处理器自动生成依赖关系(在.d文件中)并将它们包含在makefile中(因为我的课程笔记会这样说),所以不必自动维护它们。 -MMD 标志是对此负责的。我没有得到的是:什么时候生成的.d文件?甚至没有任何使用 $ {CXXFLAGS} 的命令。据推测,诸如 $ {CXX} $ {CXXFLAGS} -c xC -o xo 之类的命令将由make自动为每个目标文件推导出来,但如果这些命令生成.d文件,如果我们只通过执行生成这些.o文件的命令来了解它们,我们是不是已经通过了知道xo,yo和zo的依赖关系的点? (假设有一些.h文件,makefile会自行忽略这些规则。)

  CXX = g ++#编译器
CXXFLAGS = -g -Wall -MMD#编译器标志
OBJECTS = xo yo zo#形成可执行文件的目标文件
DEPENDS = $ {OBJECTS:.o = .d}#用.d代替.o
EXEC = a.out#可执行文件名称
$ b $ {EXEC}:$ {OBJECTS}#链接步骤
$ {CXX} $ {OBJECTS} -o $ {EXEC}

-include $ {DEPENDS}#复制文件xd,yd,zd(如果存在的话)

解决方案


假设,像 $ {CXX} $ {CXXFLAGS } -c xC -o xo 会由make自动推断出每个目标文件,但如果这些是生成.d文件的命令,我们是不是已经通过了知道xo,yo和zo的依赖关系可能是相关的,我f我们只通过执行生成这些.o文件的命令来了解它们?


你在这里是正确的。这些依赖关系在第一次运行Makefile时不存在。



但这并不重要 - 依赖信息仅在.o文件已存在时才需要,并且你已经改变了一个.h文件。第一次运行Make时,所有的.o文件都需要生成,而.d文件是同时生成的。



之后, d文件将提供依赖信息。如果标题改变了,依赖信息会告诉Make哪个.o文件需要重建。如果源文件发生更改,那么.o将始终需要重新生成,并且会同时生成更新的依赖项信息。


I know the following makefile will have the pre-processor automatically generate dependencies (in .d files) and include them in the makefile (because my course notes say so), so that they do not have to be automatically maintained. The -MMD flag is what's responsible for this. What I don't get is: At what point are the .d files generated? There isn't even any command where ${CXXFLAGS} is used. Presumably, commands like ${CXX} ${CXXFLAGS} -c x.C -o x.o will be automatically deduced by make for each of the object files, but if these are the commands that generate the .d files, wouldn't we have already passed the point where knowing the dependencies of x.o, y.o and z.o could've been relevant, if we only know them by executing the commands that generate these .o files? (Say there are .h files that the makefile would ignore if left to deduce the rules on its own or something.)

CXX = g++                     # compiler
CXXFLAGS = -g -Wall -MMD      # compiler flags
OBJECTS = x.o y.o z.o         # object files forming executable
DEPENDS = ${OBJECTS:.o=.d}    # substitutes ".o" with ".d"
EXEC = a.out                  # executable name

${EXEC} : ${OBJECTS}          # link step
    ${CXX} ${OBJECTS} -o ${EXEC}

-include ${DEPENDS}           # copies files x.d, y.d, z.d (if they exist)

解决方案

Presumably, commands like ${CXX} ${CXXFLAGS} -c x.C -o x.o will be automatically deduced by make for each of the object files, but if these are the commands that generate the .d files, wouldn't we have already passed the point where knowing the dependencies of x.o, y.o and z.o could've been relevant, if we only know them by executing the commands that generate these .o files?

You're correct here. The dependencies aren't present the first time the Makefile is run.

But this doesn't matter - the dependency information is only needed when .o files are already present, and you've changed a .h file. The first time you run Make, all .o files will need to be built anyway, and the .d files are generated at the same time.

After that, the .d files will give dependency information. If a header is changed, the dependency information will tell Make which .o files need rebuilding. If a source file is changed, the .o will always need to be rebuilt, and updated dependency information will be generated at the same time.

这篇关于在makefile中使用g ++和-MMD来自动生成依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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