Make And Target Directory


Table of Contents

make into another directory summary 
using the Explicit Path Method 
make file not in current directory 
make file not in current directory 
make file not in current directory 
make, into another directory 
make, into another directory 
make, into another directory 

make into another directory summary 

The best way to make generated into another directory is explained in Multi-Architecture Builds Using GNU make http://make.paulandlesley.org/multi-arch.html

where 3 of the following methods are discussed:

  1. Source Copy Method
  2. Explicit Path Method
  3. VPATH Method

The conclusions are:

  • VPATH is good for finding sources, not for finding targets.
  • The most convenient way for developers to use is via the VPATH Method.
  • The Explicit Path Method is much more straight forward than the VPATH Method, but it has its own problem.

Still, if you need to go for the Explicit Path Method for its simplicity, follow Paul's Fourth Rule of Makefiles http://make.paulandlesley.org/vpath.html

If you are impatient, here is the summary.

using the Explicit Path Method 

When using the Explicit Path Method, the only reliable way to construct a makefile that will place targets into a remote directory, rather than the current directory, is by prefixing all the targets with that directory path.

We can use some of GNU make's functions to make things a little simpler to read/modify. For example, for a executable program 'foo' that is generated from 'foo.c' and 'bar.c':

PROGS   = foo
OBJECTS = foo.o bar.o

# Shouldn't need to change anything below here...

OBJDIR = ../obj

VPATH = $(OBJDIR)

$(OBJDIR)/%.o : %.c
        $(COMPILE.c) $< -o $@

OBJPROG = $(addprefix $(OBJDIR)/, $(PROGS))

all: $(OBJPROG)

$(OBJPROG): $(addprefix $(OBJDIR)/, $(OBJECTS))
        $(LINK.o) $^ $(LDLIBS) -o $@

This makefile will work correctly in all situations.

Paul said it doesn't need VPATH at all — VPATH is never consulted. But I doubt that if I remove the 'VPATH = ' line, thing will still work.

T

documented on: 2006.10.24