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:
That at least explains what you are seeing. Since
prog.exedepends onlib, your phony target for collecting libraries, thelibrule is fired, and henceprog.exeis “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
progsandlibs, which in my experience are more common. In this case, thelibstarget is just a convenience for building all the libraries, rather than acting as an actual dependency.