I have a Makefile for compiling some C source files, and put the output in different Build directory, it looks a bit like the makefile below:
OBJDIR=/home/test/alpha
SRCDIR=/home/test/beta
# make variable will be exanded when used (and not at declaration)
OBJECTS:= $(addprefix $(OBJDIR), $(notdir $(patsubst %.c,%.o,$(wildcard $(SRCDIR)/*.c))))
SOURCES:=$(wildcard $(SRCDIR)/*.c))
.PHONY: copySourcesOver linkSources sharedLibrary
copySourcesOver: $(SOURCES)
cp $(SOURCES) $(OBJDIR)/
# pattern
$(OBJECTS)/%.o : $(OBJECTS)/%.c
gcc -fPIC $< -o $@
linkSources: $(OBJECTS)
gcc -shared -o libshared.so $@
sharedLibrary: copySourcesOver \
linkSources \
So what is happening is that when I run the target:
make sharedLibrary
I get no rule for making $(OBJECTS)
. The interesting part is when I make this target a second time I don't get this makefile error, and I get the shared library.
I have done a bit of research and I have found out that Makefile does not expand implicit rules for PHONY targets, that is why I have changed linkSources
from a PHONY target to a normal file. But unfortunately it does not solve the issue.
I don't see why the pattern is only executed the second time I run make sharedLibrary
-j
); there's no guarantee that prerequisites are executed in order, if one prerequisite depends on the other then express this in the makefile. – user657267make
's checking whether a rule must run can't be expected to work correctly. – reinierpost