yapkm01
yapkm01

Reputation: 3775

Makefile: Trying to understand sequence of execution

I have the following Makefile.

objects = foo.o bar.o all.o  -- line 1
all: $(objects)

# These files compile via implicit rules
foo.o: foo.c 
bar.o: bar.c
all.o: all.c

all.c:
    echo "int main() { return 0; }" > all.c

%.c:
    touch $@

clean:
    rm -f *.c *.o all

When i run make, i get the below output.

echo "int main() { return 0; }" > all.c
cc    -c -o all.o all.c
touch foo.c
cc    -c -o foo.o foo.c
touch bar.c
cc    -c -o bar.o bar.c
cc   all.o foo.o bar.o   -o all

Question:
Line 1 of the Makefile shows foo.o is the first dependency. So why all.o which depends on all.c are executed first?

Upvotes: 0

Views: 74

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

You are confusing things by having the file all.c (and object file all.o) and the target all. Conventionally, the all target is a pseudo-target (.PHONY in GNU Make) for 'all the things that are (normally) built by this makefile. This isn't enforced — it is merely convention.

However, here, make is trying to use all.c to build a program all. And it knows it can build all by compiling all.c, so it generates that first.

You could clarify things by either using any.c instead of all.c and building any.o and program any, and having the all: target read:

all: any

That would build the program any

Upvotes: 1

Related Questions