Removing middle dependency in Make
I have a makefile based build system that builds some libraries and dependencies and puts them inside a compressed archive. I have a problem where removing a middle dependency in a dependency chain only causes make to build part of the whole.
My chain looks something l开发者_如何学JAVAike this:
.PHONY: all
all: targets
objs/myprog: src/myprog.c
gcc -c $< -o $@
bin/myprog: objs/myprog
cp $< $@
archive.tar.gz: bin/myprog
cd bin && tar -czf ../archive.tar.gz *
.PHONY: targets
targets: archive.tar.gz bin/myprog objs/myprog
If I remove bin/myprog and then run make
then objs/myprog
is compiled, but it is not copied to bin/myprog
and the archive is not rebuilt. If I run make
once more, then the two missing steps are performed.
Any ideas what I might be doing wrong, or is this the intended behaviour or a shortcoming of make?
You want your final output rule to be the first (i.e. default) rule in the Makefile - this works:
archive.tar.gz: bin/myprog.o
cd bin && tar -czf ../archive.tar.gz *
objs/myprog.o: src/myprog.c
gcc -c $< -o $@
bin/myprog.o: objs/myprog.o
cp $< $@
If I remove bin/myprog and then run make then objs/myprog is compiled, but it is not copied to bin/myprog and the archive is not rebuilt. If I run make once more, then the two missing steps are performed.
I seem to recall seeing behaviour like this when some of the targets in the chain are directories. The modification time of the directory gets updated whenever anything within changes, so a directory is often more up-to-date (in the make sense) than you expect it to be.
It's usually best to rewrite such rules so that the targets and prerequisites are proper files representing the same things, even if you have to introduce a stampfile that just gets touched
to signal that the directory it represents has been brought up-to-date.
You say the makefile in your question is a precis of your real one. Does the real one have any directories as targets in this chain?
Works for me.
Just make sure your first lines are:
# don't forget an empty line at the beginning of the Makefile.
all: archive.tar.gz
精彩评论