将多个目标中的 Makefile 模式传递给依赖项

Passing Makefile pattern in multiple targets to dependency

我正在尝试根据 Makefile 中的实例化目标匹配依赖项。

我希望这段代码可以工作,但它没有。

a b: $@.test

*.test:
  echo $@

我想make a输出a.test

此外,如何访问 * 部分并简单地输出 a

为什么您认为它会起作用?

首先,你不能在像a b: $@.test这样的先决条件列表中使用$@;自动变量是 only defined inside recipes,不在先决条件列表中。所以它是空的。

其次,当 make 解析您的 makefile 时,它​​将扩展 *.test 通过查找磁盘上与该模式匹配的所有文件,并且可能没有任何文件(因为您还没有构建它们)所以这将扩展为空字符串。

基本上你的 makefile 和你写的一样:

a b: .test

:
        echo $@

如果你想 make a 输出 a.test 你可以使用 static-pattern rules 像这样:

a b : % : %.test

%.test:
        echo $@

如果您只想要与模式匹配的部分 use $* 而不是 $@.