为什么.PHONY在这种情况下不起作用? [英] Why does .PHONY not work in this situation?
问题描述
我有一个复杂的makefile,似乎每次调用它都会重新链接我的库和可执行文件.我能够将问题缩小为一个简单的makefile:
I have a complicated makefile that seems to relink my libraries and executables every time I invoke it. I was able to narrow down the problem into a simple makefile:
1: all: prog
2:
3: .PHONY: prog
4: prog: prog.exe
5:
6: prog.exe: lib prog.o
7: touch prog.exe
8:
9: prog.o: prog.c
10: touch prog.o
11:
12: .PHONY: lib
13: lib: lib.so
14:
15: lib.so: lib.o
16: touch lib.so
17:
18: lib.o: lib.c
19: touch lib.o
20:
21: .PHONY: clean
22: clean:
23: rm *.so *.o *.exe
由于某些原因,此示例每次都会创建prog.exe.如果我将第6行的lib替换为lib.so,那么它将起作用.但是看来我应该能够做我在这里尝试的事情.我缺少什么基本的东西吗?
For some reason, this example, prog.exe is created every time. If I replace the lib on line 6 with lib.so then it works. Yet it seems like I should be able to do what I'm attempting here. Is there something fundamental I'm missing?
推荐答案
从在线GNU制作手册中:
From the online GNU make manual:
假冒目标不应是 实际目标文件的前提条件;如果 是的,它的配方每次都会运行 时间使去更新该文件.作为 只要一个虚假的目标永远不会 真正目标的先决条件 虚假目标配方将被执行 仅当虚假目标为 指定的目标(请参阅 指定目标).
A phony target should not be a prerequisite of a real target file; if it is, its recipe will be run every time make goes to update that file. As long as a phony target is never a prerequisite of a real target, the phony target recipe will be executed only when the phony target is a specified goal (see Arguments to Specify the Goals).
至少可以解释您所看到的内容.由于prog.exe
依赖于lib
(您用于收集库的伪造目标),因此会触发lib
规则,因此prog.exe
为过时"并重新链接.
That at least explains what you are seeing. Since prog.exe
depends on lib
, your phony target for collecting libraries, the lib
rule is fired, and hence prog.exe
is "out of date", and gets relinked.
看来您必须更加明确地了解您的依赖项.也许您有兴趣将它们放在一个变量中,以使管理多个库变得更加容易?例如:
It looks like you'll have to be more explicit about your dependencies. Perhaps you would be interested in putting them in a variable to make managing multiple libraries a bit easier? For example:
LIBS = lib.so
all: libs progs
.PHONY: progs libs clean
progs: prog.exe
prog.exe: $(LIBS) prog.o
touch prog.exe
# etc.
libs: $(LIBS)
lib.so: lib.o
touch lib.so
# and so on
在上面的示例中,我还将伪造目标的名称更改为progs
和libs
,根据我的经验,这更常见.在这种情况下,libs
目标只是构建所有库的便利,而不是充当实际的依赖项.
In the above example, I've also changed the names of the phony targets to progs
and libs
, which in my experience are more common. In this case, the libs
target is just a convenience for building all the libraries, rather than acting as an actual dependency.
这篇关于为什么.PHONY在这种情况下不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!