help-make
[Top][All Lists]
Advanced

[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.

http://www.gnu.org/software/make/manual/html_node/General-Search.html
http://www.gnu.org/software/make/manual/html_node/Selective-Search.html

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

http://www.gnu.org/software/make/manual/html_node/Pattern-Rules.html

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:

emp\Database.cpp
emp\Database.h
emp\DatabaseTest.cpp
emp\Employee.cpp
emp\Employee.h
emp\EmployeeTest.cpp
GNUmakefile

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
        $(COMPILE.cc) $*.cpp -o $*.o

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

.PHONY: clean

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 COMPILE.cc
  COMPILE.cc = $(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.
endif

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

$(EXPF)%.o: %.cpp
        $(COMPILE.cc) $*.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]