宣布所有为.phony的目的?
GNU制作手册描述以下 > .phony 。
all : prog1 prog2 prog3
.PHONY : all
prog1 : prog1.o utils.o
cc -o prog1 prog1.o utils.o
prog2 : prog2.o
cc -o prog2 prog2.o
prog3 : prog3.o sort.o utils.o
cc -o prog3 prog3.o sort.o utils.o
它指出,我现在只能调用制作
重新制定所有三个程序。这很好,但是我可以实现相同的目标,而无需声明所有作为的先决条件。因此,我的问题是:
的目的
被列为.phony
的先决条件,即使我仍然可以重新制作所有三个程序在不太可能的情况下,我偶然地在同一目录中创建一个名为all
的文件?我很想知道其他原因,谢谢。
The GNU Make manual describes the following use case of .PHONY
.
all : prog1 prog2 prog3
.PHONY : all
prog1 : prog1.o utils.o
cc -o prog1 prog1.o utils.o
prog2 : prog2.o
cc -o prog2 prog2.o
prog3 : prog3.o sort.o utils.o
cc -o prog3 prog3.o sort.o utils.o
It states that I can now just call make
to remake all three programs. Which is fine, but I can achieve the same without declaring all
as a prerequisite of .PHONY
(as all
is the default goal). Hence, my question:
Is the only purpose of all
being listed as a prerequisite of .PHONY
that I can still remake all three programs even if I accidentally, in the unlikely case, create a file in the same directory named all
? I would love to know other reasons if there are any, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎怀疑,但是是的。这是在该示例中声明 phony 的唯一目的。
如您链接的手册部分所述:
就是这样。这两个结果均来自
make
不根据该目标的名称对应于现有文件的名称,或者在该文件的时间戳上相对于任何先决条件的时间戳,而不是对伪造目标的配方执行的执行。 。它甚至没有寻找这样的文件。这就是宣布目标假冒的实际效果的全部程度。还要注意,尽管在示例中使用
.phony
的优点似乎很小 - 为什么要保护像名为“ All”已添加到目录中的文件的不太可能事件? - 我们确实会不时在这里提出问题,最终是由于(非)发音的问题引起的。因此,请帮个忙,并在适当的情况下声明目标。这并不难,它可能只会为您,您的团队或您的客户挽救不相同的悲伤。You seem doubtful, but yes. That is the only purpose served by declaring
all
phony in that example.As the manual section you linked explains:
That's it. Both results derive from
make
not conditioning execution of the recipe for a phony target on whether the target corresponds to the name of an existing file, or if it does, on the timestamp of that file relative to any prerequisites. It doesn't even look for such a file. That's the full extent of the practical effect of declaring a target phony.Note also that although the advantage of using
.PHONY
in an example such as the one presented may seem small -- why protect against such an unlikely event as a file named "all" being added to the directory? -- we do get questions here from time to time that end up arising from issues with (non-)phoniness. So do yourself a favor, and declare your targets.PHONY
when that's appropriate. It's not hard, and it might just save you, your team, or your customers an incommensurate amount of grief.实际上,因为没有“全部”的配方,因此即使不宣布为“伪造”,事情也可以很好地工作。
根据Makefile的规则链,如果发生了变化,“ Make All”将最终更新目标“ All”的相应先决条件。
无需将“全部”称为“假冒”,因为我们只希望及时更新目标“全部”的先决条件。
Actually, cause there is no recipe for 'all', so even without declaring as 'PHONY', things can also work well.
According the rule chain of Makefile, if there is something has changed, 'make all' will update the corresponding prerequisites of the target 'all' finally.
It is no need to declare 'all' as 'PHONY', because we just want the prerequisites of the target 'all' to be updated in a timely manner.