The following Makefile somtimes fails to build the main target (app1). Can anyone reproduce the error and explain what’s wrong?
— Makefile start —
AR := ar
CC := gcc
all: app1
Link apps
app%: app%.o lib.a
${CC} ${@:app%=app%.o} lib.a -o $@
Link lib.a
LIBSRCS := a b
lib.a: ${LIBSRCS:%=%.o}
${AR} r $@ ${LIBSRCS:%=%.o}
Compile
%.o: src/%.c
$(CC) -c -o $@ $<
Dependencies
a.o: a.c lib.h
b.o: b.c lib.h
— Makefile end —
— app1.c start —
int main() { return 0; }
— app1.c end —
— a.c start —
void a() {}
— a.c end —
— b.c start —
void b() {}
— b.c end —
— lib.h start —
— lib.h end —
To reproduce the error, touch lib.h and run make in parallel mode with -j2, e.g.,
while sleep 2 && touch lib.h && make -j 2 app1 ; do echo ; done
On my machine, the above command fails after a few loops. From the output it seems that make tries to link app1 before compiling app1.o.
openSuSE 11.3, make 3.31, Intel core i5 551.