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.
In the GNU Make program I am using built for mingw64 (windows),
GNU Make 4.2.1 Built for x86_64-w64-mingw32
I am able to use this target with this command,
debug:
cd $(PREFIX) && $(GDB) kiwigb.exe
The results are the directory change is temporary, but all works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With