makefile - 不执行虚假目标的配方

标签 makefile

考虑从 GNU make 手册中摘录的 PHONY 目标:

Once this is done, make clean' will run the commands regardless of whether there is a file namedclean'.

为什么我没有看到我的食谱被执行,尽管在调试输出中 make 说它必须重新制作这些目标?下面是一个示例 makefile 来演示该问题:

all:
    @echo Rule to build 'all'

.PHONY: clean a.clean b.clean
clean: a.clean b.clean
    @echo Cleaning toplevel

%.clean:
    @echo Cleaning $*

这是输出 - 注意没有提到 Cleaning $*

make: Entering directory `/tmp'
Cleaning toplevel
make: Leaving directory `/tmp'

这是 -d 的输出:

Considering target file `clean'.
 File `clean' does not exist.
  Considering target file `a.clean'.
   File `a.clean' does not exist.
   Finished prerequisites of target file `a.clean'.
  Must remake target `a.clean'.
 Successfully remade target file `a.clean'.
  Considering target file `b.clean'.
   File `b.clean' does not exist.
   Finished prerequisites of target file `b.clean'.
  Must remake target `b.clean'.
  Successfully remade target file `b.clean'.
 Finished prerequisites of target file `clean'.
Must remake target `clean'.

最佳答案

来自 the same section of the manual :

因为它知道假目标不会命名可以从其他文件重新制作的实际文件,make 会跳过隐式规则搜索假目标(请参阅隐式规则)。

是的,Make 知道它必须“重新制作”a.cleanb.clean,但是在寻找这样做的规则时,它不会考虑您的模式规则。它找不到这些目标的配方,所以它耸了耸肩,认为工作完成并继续前进。

您可以通过将模式规则设为静态 模式规则来解决此问题:

a.clean b.clean : %.clean :
    @echo Cleaning $*

关于makefile - 不执行虚假目标的配方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19659273/

相关文章:

c++ - 尝试在 QT 中编译项目时出现 Jom 错误

c - asm 关键字作为 C 中的标识符名称?

c++ - boost 程序选项 : linking error

c++ - sclite (SCTK),C++ 模板参数 Filter::Filter* 无效。 Cygwin

macos - 无法制作 OpenCV

makefile - 如何在 Cmake QtCreator 项目中为 make 设置多个并行作业?

linux - gnu makefile 变量的值,但前提是它尚未包含它

c - 如果没有以下 make 文件,我该如何手动执行?

android - 如何强制 Cmake 执行多组件依赖的顶级 CMakeLists.txt?

c++ - 将外部文件添加到 makefile