0

I've been using Makefile and to keep some stuff sepparate I decided to include a new common makefile. The problem I faced is that my First makefile looks like this:

Filename Makefile.test

include ./Makefile.a
all: a b c
b:
    @echo "b"
c:
    @echo "c"

Filename Makefile.a

a:
    @echo "a"

When I execute my makefile: make -f Makefile.test I only get "a" printed out and it finishes.

The only way to make it work is place the include bellow (anywhere) the all: target

Is there a reason why this behaves liks this?

Thanks!

1 Answer 1

10

When you say this:

include ./Makefile.a

make just pulls Makefile.a into the current Makefile in the same way that #include <x.h> does in C: it inserts the contents of Makefile.a to replace the include statement and keeps going. The result is that make sees this when it starts figuring out what to do:

a:
    @echo "a"
all: a b c
b:
    @echo "b"
c:
    @echo "c"

So the first target in source-order will be a and since you didn't specify a target, make will use the first one. That's where your result comes from.

You could explicitly specify the all target if you wanted:

make -f Makefile.test all

That would use the all target regardless of where it appears in the Makefile.

If you're using GNU Make, then you could use .DEFAULT_GOAL to specify a default target inside Makefile.test and then you wouldn't have to worry about the order:

include ./Makefile.a
all: a b c
b:
    @echo "b"
c:
    @echo "c"

.DEFAULT_GOAL := all

Thanks to Idelic for the reminder about this.

2
  • 3
    If using GNU make, you could also override the default target in Makefile.test by setting .DEFAULT_GOAL := all.
    – Idelic
    Oct 4, 2012 at 15:07
  • @Idelic: Thanks for the reminder, been awhile since I've done any serious make work. Oct 4, 2012 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.