makefile - 在makefile中使用g++和-MMD自动生成依赖

标签 makefile dependencies g++

我知道以下 makefile 将使预处理器自动生成依赖项(在 .d 文件中)并将它们包含在 makefile 中(因为我的类(class)笔记是这样说的),因此不必自动维护它们。 -MMD标志是对此负责的。我不明白的是: .d 文件是在什么时候生成的?甚至没有任何命令在 ${CXXFLAGS}用来。据推测,像 ${CXX} ${CXXFLAGS} -c x.C -o x.o 这样的命令将由 make 自动为每个目标文件推断,但如果这些是生成 .d 文件的命令,我们不是已经过了知道 x.o、y.o 和 z.o 的依赖关系的地步吗? ,如果我们只通过执行生成这些 .o 文件的命令来了解它们? (假设有 .h 文件,如果让其自行推断规则或其他东西,makefile 将忽略这些文件。)

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?



你在这里是对的。第一次运行 Makefile 时不存在依赖项。

但这无关紧要 - 仅当 .o 文件已经存在并且您更改了 .h 文件时才需要依赖信息。第一次运行 Make 时,无论如何都需要构建所有 .o 文件,同时生成 .d 文件。

之后,.d 文件将给出依赖信息。如果一个头被改变,依赖信息会告诉 Make 哪些 .o 文件需要重建。如果源文件发生变化,总是需要重新构建 .o,同时会生成更新的依赖信息。

关于makefile - 在makefile中使用g++和-MMD自动生成依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11855386/

相关文章:

linux - 安装 glib : error: redefinition of typedef 'GListStore'

c - 在 Makefile 中获取源代码结构

java - 在 Play Framework 应用程序中管理依赖项

html - 如何快速填充 Rails 中的 *data inter-dependent* <select> 下拉菜单?

c++ - G++ 警告 : built for unsupported file format which is not the architecture being linked

c++ - 如何在不放置任何物理依赖项的情况下指定 Makefile 目标构建顺序?

c++ - gdb : what is a symbol-file used for debugging

scala - SBT 子项目相互依赖关系

linux - 我可以强制编译器将我的共享库也放入二进制文件中吗?

c++ - g++ 奇怪的警告