[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

VPATH/vpath and Pattern Rules

From: Michael Ludwig
Subject: VPATH/vpath and Pattern Rules
Date: Sat, 14 Jan 2012 13:46:22 +0100
User-agent: Mutt/1.5.20 (2009-12-10)

How can you convince Make to honour the VPATH/vpath when using a Pattern
Rule defined in my Makefile (as opposed to the built-in pattern rule)?

Here's the full story:

You can instruct Make to search directories other than the working
directory for prerequisites by specifying those directories in VPATH
or vpath.

And you can define Pattern Rules to avoid listing files explicitly.

Just how do VPATH/vpath and Pattern Rules work together?

I was puzzled to see that Make wouldn't feed the correct file paths to
the compiler for the following setup:


The GNUmakefile being:

CC       = gcc.exe
CXX      = g++.exe
CFLAGS   = -Wall -s
CXXFLAGS = $(CFLAGS) -std=c++0x

# VPATH = emp
vpath Employee%.cpp emp
vpath Database%.cpp emp

EXEN = EmployeeTest.exe DatabaseTest.exe

all: $(EXEN)

# Pattern Rule
%.o: %.cpp
        $( $*.cpp -o $*.o

EmployeeTest.exe: EmployeeTest.o Employee.o
        $( $^ -o $@
DatabaseTest.exe: DatabaseTest.o Employee.o Database.o
        $( $^ -o $@

.PHONY: clean

        del $(EXEN) *.o

Typing make results in the following:

g++.exe -Wall -s -std=c++0x   -c EmployeeTest.cpp -o EmployeeTest.o
g++.exe: error: EmployeeTest.cpp: No such file or directory
g++.exe: fatal error: no input files
compilation terminated.
make: *** [EmployeeTest.o] Error 1

Now, remove the Pattern Rule and it works perfectly. The pattern rule
is (almost) redundant in my example:

  $ make -p | findstr = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c

My real pattern rule, however, involves a renaming of object files and
executables according to whether they're being compiled with MinGW or
MinGW-w64 so that I can use the same Makefile for both compilers:

EXPF     = mgw.

ifdef X64
CC       = D:\Opt\MinGW64\bin\gcc.exe
CXX      = D:\Opt\MinGW64\bin\g++.exe
EXPF     = m64.

EXEN = $(EXPF)EmployeeTest.exe $(EXPF)DatabaseTest.exe

$(EXPF)%.o: %.cpp
        $( $*.cpp -o $(EXPF)$*.o

And then, Make won't find the CPP files despite the VPATH/vpath.

How can you convince Make to honour the VPATH/vpath when using my
pattern rule (as opposed to the built-in pattern rule)?

Michael Ludwig

reply via email to

[Prev in Thread] Current Thread [Next in Thread]