makefile - GNU make 总是将非文件目标视为正在重制(可能是错误?)

标签 makefile gnu-make

是否应该Make始终重新制作具有先决条件但不指向文件且没有配方的目标?或者只有在需要重新制定一个或多个先决条件时才应制定重新制定此类目标?

例如,在下面所示的 Makefile 中,我添加了一个名为 objects 的目标,该目标具有先决条件,但不指向文件。 主要目标称为程序,它依赖于对象(以及可能的其他先决条件)。

file1 file2 file3: ;       touch $@

objects: file1 file2

program: objects  file3 ;  @echo 'Making $@'

我的期望是,当 make program 运行并且不需要重新制作文件先决条件(即 file1、file2 和 file3 都存在)时,该 program 不应该重制。

然而,实际的 GNU Make 行为是 program 的配方始终运行(无论先决条件文件如何)。这是因为对象始终被认为是重新制作的,这会强制重新制作任何依赖项(即程序)。

您可以通过运行来验证这一点:make --trace -d --no-builtin-rules program,您将看到 Make 始终输出“必须重新制作目标“对象”。” 所以objects总是被“重制”(即使它没有配方)并且它总是被认为是新更新的。

这很可能是因为 objects 没有指向真实的文件。但我预计,因为它没有配方,只要不需要重新制作它的先决条件,它就不会被重新制作。

这是预期行为还是错误?

GNU Make manual状态:

If a rule has no prerequisites or recipe, and the target of the rule is a nonexistent file, then make imagines this target to have been updated whenever its rule is run. This implies that all targets depending on this one will always have their recipe run.

但该描述不适用于上述情况,因为 objects 目标确实有先决条件。但也许行为是相同的,并且应该更新 Make 手册来澄清这一点。

一些附加说明:

  • 我在上面使用了分号后的配方格式,以便更容易 读者复制并粘贴代码。否则,当从 网页中,制表符将转换为通常的空格 制表符缩进配方格式。

  • 当然我知道我可以创建一个变量 $(OBJECTS) 将指向文件列表并使用它代替对象 目标同上。这不是真正的重点。

  • This question似乎也涉及相同的行为,但没有讨论它是预期行为还是错误。

  • 我正在使用 GNU Make 4.2.1

最佳答案

我相信您引用的段落只是重述了其他地方已经指定的特定行为案例,以便为 FORCE 的工作方式设置场景。

事实上,previous section 中提出了一个更简单但更一般的声明。 :

If you write a rule whose recipe will not create the target file, the recipe will be executed every time the target comes up for remaking.

再次here :

A target is out of date if it does not exist or if it is older than any of the prerequisites.

所以您所观察到的行为完全是预料之中的。

关于makefile - GNU make 总是将非文件目标视为正在重制(可能是错误?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47932338/

相关文章:

linux - Makefile 规则未执行

c - 与 cygwin 链接

makefile - GNU 制造商 : % doesn't match dotted filenames?

multithreading - GNU make-如何模拟多个同时作业

java - 如果文件不存在,如何使输出流创建文件?

android - 如何为 NDK_MODULE_PATH 指定目录

gnu-make - 为什么 GNU Make jar 头食谱不起作用?

c++ - Makefile 在运行时无法链接库

makefile - 为什么 make 不检测 header 依赖项的变化

makefile - 在 Makefile 本身中包含 make 的参数