GNU make manual 翻译(九十九)

本文介绍了在Make中如何正确地使用Phony目标来避免因同名文件导致的目标更新问题,并解释了为什么声明Phony目标有助于提升构建性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继续翻译

复制代码
   The phony target will cease to work if anything ever does create a
file named `clean' in this directory.  Since it has no prerequisites,
the file `clean' would inevitably be considered up to date, and its
recipe would not be executed.  To avoid this problem, you can explicitly
declare the target to be phony, using the special target `.PHONY'
(*note Special Built-in Target Names: Special Targets.) as follows:

     .PHONY : clean

Once this is done, `make clean' will run the recipe regardless of
whether there is a file named `clean'.

   Since it knows that phony targets do not name actual files that
could be remade from other files, `make' skips the implicit rule search
for phony targets (*note Implicit Rules::).  This is why declaring a
target phony is good for performance, even if you are not worried about
the actual file existing.
复制代码

如果在目录下创建了名为 clean 的文件,则此伪目标将停止工作。因为它没有前提条件,文件clean 会被认为是最新的,它的片段不会被执行。为了回避这个问题,你可以显式声明此目的为伪--使用特殊的目的--.PHONY(*note Special Built-in Target Names: Special Targets.),如下;

.PHONY : clean

当这个做好以后,make clean 无论 是否有一个名为 clean 的文件存在,都会执行片段。

由于make 知道,伪目标不会命名从其他文件而重新创建的文件名,它会跳过对伪目标的隐式规则搜索(*note Implicit Rules::),这是为何定义伪目标有利于提高性能,甚至你根本不需担心是否实际的文件存在。

后文待续


本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/09/20/2694748.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值