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:
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".
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:-
$(addprefix $(blddir)/,$(srcs:.c=.o))
$(blddir)/%.o: %.c | $(blddir)
There can be powerful reasons to make make
change its working directory but
merely putting build products in a separate directory isn't one.