How to change current directory in GNU Make

ruvim picture ruvim · May 26, 2016 · Viewed 15.5k times · Source

I want to separate the directory with sources from the directory with targets. And it seems that changing the current working directory from Makefile should be the simplest solution.

Explicit path to targets is not sufficient because of the following drawbacks:

  1. Redundant code in Makefile since every reference to target should be prefixed with variable.
  2. More complex command line to build particular intermediate target (worse for debugging).

See also Pauls's rule #3:

Life is simplest if the targets are built in the current working directory.

Regarding VPATH — I also agree that requiring developers "to change to the target directory before running make is a pain".

Answer

Mike Kinghan picture Mike Kinghan · May 26, 2016

Building targets in a separate directory is a commonplace make practice that GNU make conveniently supports without changing directory or invoking auxiliary tools. Here is a routine illustration:

Makefile

srcs := main.c foo.c
blddir := bld
objs := $(addprefix $(blddir)/,$(srcs:.c=.o))
exe := $(blddir)/prog

.PHONY: all clean

all: $(exe)

$(blddir):
    mkdir -p $@

$(blddir)/%.o: %.c | $(blddir)
    $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

$(exe) : $(objs)
    $(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)

clean:
    rm -fr $(blddir)

which runs like:

$ make
mkdir -p bld
cc   -c -o bld/main.o main.c
cc   -c -o bld/foo.o foo.c
cc -o bld/prog bld/main.o bld/foo.o

Cribs:-

There can be powerful reasons to make make change its working directory but merely putting build products in a separate directory isn't one.