gnu-make - makefile 中的自动依赖生成

标签 gnu-make makefile multiple-makefiles

我试图了解如何在给定链接的 makefile 中生成自动依赖关系,我无法理解以下代码:

DEPDIR = .deps
df = $(DEPDIR)/$(*F)

SRCS = foo.c bar.c ...

%.o : %.c
        @$(MAKEDEPEND); \
          cp $(df).d $(df).P; \
          sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
              -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \
          rm -f $(df).d
        $(COMPILE.c) -o $@ $<

-include $(SRCS:%.c=$(DEPDIR)/%.P)

我从 this link. 得到的我知道它会生成依赖文件,但我无法理解这一行的作用:

sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
    -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \

有人可以解释一下这段代码吗,这么多通配符让我很困惑,我是 makefile 的新手。

最佳答案

这是许多不同的命令,因此请将其分解。

  1. -e 's/#.*//'

    删除以 # 开头的所有内容(注释?预处理器指令?)

  2. -e 's/^[^:]*: *//'

    删除任何具有 : 的内容,直至 :

  3. -e 's/*\\$$//'

    从行尾删除续行斜杠(及其前面的空格)。

  4. -e '/^$$/d'

    删除所有空行。

  5. -e 's/$$/:/'

    : 添加到每行末尾。

这为每个列出的依赖文件添加了显式目标,以便 make “知道”如何构建它们,以避免“没有规则来创建目标”错误。这里的推理在 an earlier section 中的链接中进行了解释。 .

Briefly, this creates a .P file with the original prerequisites list, then adds targets to it by taking each line, removing any existing target information and any line continuation (\) characters, then adding a target separator (:) to the end. This works with the values for MAKEDEPEND I suggest below; it’s possible you will need to modify the translation for other dependency generators you might use.

关于gnu-make - makefile 中的自动依赖生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30669884/

相关文章:

makefile - 从makefile中调用make

无法在 Gnu-Make 中禁用 "default"后缀规则

compiler-construction - 无法编译 C++ 项目(宏 "max"传递了 3 个参数,但只需要 2 个)

gcc - 如何强制 make/GCC 显示命令?

makefile - 多个目标的构建后步骤

安卓NDK : build after added a new library module directly under jni/folder

makefile - 在 makefile 目标的先决条件中出现分号是什么意思?

makefile - Makefile 中的链接模式规则

c++ - Makefile 需要运行定义两次,一次添加到计数器,第二次编译