没有配方的 makefile 模式规则

发布于 2024-09-24 05:03:16 字数 636 浏览 6 评论 0原文

我正在观察 make 的一个有趣的行为,我想知道除了 gmake 中的错误之外是否还有合理的解释。

假设我们在 makefile 中有以下内容:

%-animal:
        echo "$* is an animal"

%-fox: %-fox-animal

%-wolf: %-wolf-animal

最后两个目标之间的区别是“%-wolf”没有任何配方,而“%-fox”有一个空配方(即开头有一个制表符的行) )。

当我们尝试执行规则时,会发生以下情况:

[root@cv19 tmp]# make freddy-animal
echo "freddy is an animal"
freddy is an animal
[root@cv19 tmp]# make freddy-wolf
make: *** No rule to make target `freddy-wolf'.  Stop.
[root@cv19 tmp]# make freddy-fox
echo "freddy-fox is an animal"
freddy-fox is an animal

即具有配方(尽管是空的)的模式规则有效,而没有配方的模式规则则无效。我是否错过了它应该工作的方式?

I'm observing an interesting behavior of make and I wonder if there is a reasonable explanation to it besides a bug in gmake.

Let's say we have the following in makefile:

%-animal:
        echo "$* is an animal"

%-fox: %-fox-animal

%-wolf: %-wolf-animal

The difference between the last two targets is that "%-wolf" does not have any recipe, and "%-fox" has an empty recipe (i.e. just a line with a tab at the beginning).

When we try to execute the rules, here's what happens:

[root@cv19 tmp]# make freddy-animal
echo "freddy is an animal"
freddy is an animal
[root@cv19 tmp]# make freddy-wolf
make: *** No rule to make target `freddy-wolf'.  Stop.
[root@cv19 tmp]# make freddy-fox
echo "freddy-fox is an animal"
freddy-fox is an animal

i.e.the pattern rule that has a recipe (although an empty one) works, the one that doesn't does not. Am I missing something in the way it's supposed to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-10-01 05:03:16

根本没有配方的模式规则被记录为与提供配方(甚至是空配方)的含义完全不同的东西。相反,他们取消任何预先存在的隐式规则

您可以通过定义具有相同目标和先决条件但没有配方的模式规则来取消内置隐式规则。

因此,您的“%-wolf”模式实际上用于取消 %-wolf-animal 的任何现有隐式规则 -> %-狼。反正也没有。

Pattern rules with no recipes at all are documented as meaning something quite different from those providing a recipe, even an empty one. Instead they cancel any pre-existing implicit rule:

You can cancel a built-in implicit rule by defining a pattern rule with the same target and prerequisites, but no recipe.

Thus your "%-wolf" pattern actually serves to cancel any existing implicit rule for %-wolf-animal -> %-wolf. And there wasn't one anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文