开发者

Why does .PHONY not work in this situation?

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜