为什么 .PHONY 在这种情况下不起作用?
我有一个复杂的 makefile,似乎每次调用它时都会重新链接我的库和可执行文件。我能够将问题缩小到一个简单的 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。如果我用 lib.so 替换第 6 行的 lib,那么它就可以工作。然而,似乎我应该能够做我在这里尝试的事情。我缺少什么基本的东西吗?
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自在线 GNU make 手册:
这至少解释了你所看到的。由于
prog.exe
依赖于lib
(您收集库的虚假目标),因此会触发lib
规则,因此prog.exe
是“过时的”,并且被重新链接。看来您必须更明确地了解您的依赖关系。也许您有兴趣将它们放入变量中以使管理多个库更容易一些?例如:
在上面的示例中,我还将虚假目标的名称更改为
progs
和libs
,根据我的经验,这更常见。在这种情况下,libs
目标只是为了方便构建所有库,而不是充当实际的依赖项。From the online GNU make manual:
That at least explains what you are seeing. Since
prog.exe
depends onlib
, your phony target for collecting libraries, thelib
rule is fired, and henceprog.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:
In the above example, I've also changed the names of the phony targets to
progs
andlibs
, which in my experience are more common. In this case, thelibs
target is just a convenience for building all the libraries, rather than acting as an actual dependency.