Pete Wilson
Pete Wilson

Reputation: 8694

makefile confusions

I'm trying to use a top-level makefile. It's supposed to cd into 8 or 10 subdirectories, one by one, and make in each one. But I can't get it to work.

Here's the simplified makefile that lives in the top-level directory:

/home/calls/cgi>cat makefile

all:    /home/calls/cgi/chain/chain.o
        cd /home/calls/cgi/chain
        make -f /home/calls/cgi/chain/makefile

And here's the output from a make command:

/home/calls/cgi>make
cd /home/calls/cgi/chain
make -f /home/calls/cgi/chain/makefile
make[1]: Entering directory `/home/calls/cgi'
make[1]: *** No rule to make target `chain.c', needed by `chain'.  Stop.
make[1]: Leaving directory `/home/calls/cgi'
make: *** [all] Error 2
/home/calls/cgi>

What am I doing wrong, please?

Thanks!

Upvotes: 1

Views: 946

Answers (1)

geekosaur
geekosaur

Reputation: 61369

Each line in the makefile is run in its own shell, so the cd command only affects the line it's on. You can, however, use continuation lines:

all:    /home/calls/cgi/chain/chain.o
        cd /home/calls/cgi/chain; \
        make -f /home/calls/cgi/chain/makefile

or just combine them on the same line:

all:    /home/calls/cgi/chain/chain.o
        cd /home/calls/cgi/chain; make -f /home/calls/cgi/chain/makefile

or if you know that make will always be GNU make:

all:    /home/calls/cgi/chain/chain.o
        make -C /home/calls/cgi/chain -f /home/calls/cgi/chain/makefile

Also, the -f is probably redundant, if you're in the correct directory: make will try to load GNUmakefile (GNU make only), Makefile, and makefile in that order.

Upvotes: 2

Related Questions