为什么 .PHONY 在这种情况下不起作用?

发布于 2024-10-14 09:21:18 字数 549 浏览 0 评论 0原文

我有一个复杂的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木槿暧夏七纪年 2024-10-21 09:21:18

来自在线 GNU make 手册:

虚假目标不应该是
真实目标文件的先决条件;如果
是的,它的配方将每隔
是时候更新该文件了。作为
只要虚假目标永远不是
确定实际目标的前提条件是
虚假目标配方将被执行
仅当虚假目标是
指定目标(参见参数
指定目标)。

这至少解释了你所看到的。由于 prog.exe 依赖于 lib(您收集库的虚假目标),因此会触发 lib 规则,因此 prog.exe 是“过时的”,并且被重新链接。

看来您必须更明确地了解您的依赖关系。也许您有兴趣将它们放入变量中以使管理多个库更容易一些?例如:

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

在上面的示例中,我还将虚假目标的名称更改为 progslibs,根据我的经验,这更常见。在这种情况下,libs 目标只是为了方便构建所有库,而不是充当实际的依赖项。

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).

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文