使用``%''作为目标时,GNU Make的令人惊讶的(?)行为 [英] a surprising (?) behaviour of GNU Make when using ``%`` as target

查看:121
本文介绍了使用``%''作为目标时,GNU Make的令人惊讶的(?)行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下Makefile

foo:
    @echo '$@'

test:
    @echo '$@'
    @echo '---'

# Catch-all target
%: test
    @echo '+++'
    @echo '$@'

发出make bar时,控制台输出如下:

$ make bar
test
---
+++
Makefile
+++
bar

我想了解Makefile的起源,它表明在某些时候它是作为参数接收的,以及如何在这种方案中摆脱它.这是使用

GNU Make 4.1
Built for x86_64-apple-darwin13.4.0

解决方案

GNU make将makefile本身视为需要更新的目标.请参见如何重新制作Makefile :

...读完所有makefile文件后,make会将每个文件视为目标目标并尝试对其进行更新.如果一个makefile有一条规则说明如何更新它(在那个makefile或另一个makefile中找到),或者如果一个隐式规则适用于它(请参见使用隐式规则),则将在必要时进行更新...

如果您知道无法重新生成一个或多个生成文件,并且希望避免对它们执行隐式规则搜索(也许出于效率原因),则可以使用任何常规方法来防止对隐式规则进行查找,这样做.例如,您可以编写一个以makefile为目标的明确规则,并编写一个空配方(请参阅使用空配方).

因此,使用包罗万象的目标%来更新Makefile.

Makefile通常不需要更新,因此通常为此添加一个空规则:

Makefile : ;

Consider the following Makefile

foo:
    @echo '$@'

test:
    @echo '$@'
    @echo '---'

# Catch-all target
%: test
    @echo '+++'
    @echo '$@'

When issuing make bar the following is the console output:

$ make bar
test
---
+++
Makefile
+++
bar

I would like to understand the origin of Makefile which shows it is received as argument at some point, and also how to get rid of it in such a scheme. This is using

GNU Make 4.1
Built for x86_64-apple-darwin13.4.0

解决方案

GNU make treats the makefile itself as a target that needs to be updated. See How Makefiles Are Remade:

... after reading in all makefiles, make will consider each as a goal target and attempt to update it. If a makefile has a rule which says how to update it (found either in that very makefile or in another one) or if an implicit rule applies to it (see Using Implicit Rules), it will be updated if necessary...

If you know that one or more of your makefiles cannot be remade and you want to keep make from performing an implicit rule search on them, perhaps for efficiency reasons, you can use any normal method of preventing implicit rule look-up to do so. For example, you can write an explicit rule with the makefile as the target, and an empty recipe (see Using Empty Recipes).

Hence, the catch-all-target % is used to update Makefile.

Makefiles often do not have to be updated, so it is customary to add an empty rule for that:

Makefile : ;

这篇关于使用``%''作为目标时,GNU Make的令人惊讶的(?)行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆