I have this Makefile (and GNU Make):
vpath %.o .objs
OBJDIR=.objs
all: symbol_tests.so
symbol_tests.so: symbol_tests.o symbol.o # simulate linking
@echo Linking target: $@, prerequisites: $^
%.o: %.c | $(OBJDIR)/
gcc -o $(OBJDIR)/$@ -c $<
$(OBJDIR)/:
-mkdir $@
but building (without any subdir or objects existing) give me this:
mkdir .objs/
gcc -o .objs/symbol_tests.o -c symbol_tests.c
gcc -o .objs/symbol.o -c symbol.c
Linking target: symbol_tests.so, prerequisites: symbol_tests.o symbol.o
It is obvious that since neither symbol.o
nor symbol_test.o
exists they need to be built.
And they are correctly placed in the subdir ./objs
.
But when "linking" the vpath
does not pick up the newly created .o
-files in the .objs
subdir.
Isn't this strange? Running make
again gives:
Linking target: symbol_tests.so, prerequisites: .objs/symbol_tests.o .objs/symbol.o
To me that looks like that in order to trigger the search for the .o
in the subdir they have to exist when make
is invoked. That kind of defeats the purpose of vpath
, doesn't it?
Or is there something wrong with my Makefile
or understanding of vpath
?
NOTE: having $(OBJDIR) in the vpath directive does not work. Why?