tags:

views:

39

answers:

1

If I have a rule like this in my make file:

CC = g++
CFLAGS = -Wall
COMPILE = $(CC) $(CFLAGS) -c

src = A.cpp \
      main.cpp

test_src = Test.cpp 
test = testAll

OBJFILES := $(patsubst %.cpp,%.o,$(src))
TEST_OBJS := $(patsubst %.cpp,%.o,$(test_src))

%.o: %.cpp
    $(COMPILE) -I UnitTest++/src -LUnitTest++/ -l UnitTest++ -o $@ $< 

I end up including and linking UnitTest++ to non test files such as main and A.cpp How can I make only make Test classes link with the test libraries?

+2  A: 

Specify which files your rule applies to:

$(TEST_OBJS): %.o: %.cpp
    $(COMPILE) -I UnitTest++/src -LUnitTest++/ -l UnitTest++ -o $@ $<
Steve Jessop
gmake specific?
dmckee
@dmckee I just ran it using gnu make. Works. So not gmake specific.
anio
@anio: gmake denotes the GNU version. That name is often for installing GNU make when a system make already exists (admittedly this is not common these days). I've just checked the documentation for GNU make and learned that some other makes support % based pattern rules, so I still don't no.
dmckee
@dmckee: After a brief look at POSIX `make`, I guess so (and for that matter the questioner's rule is not required by POSIX to be supported, since the meaning of % in targets is implementation-defined). GNU make is the only version I've ever used, so I'd never previously investigated what POSIX says about `make`. Didn't occur to me that someone using `g++` as their compiler might be using any other `make` :-)
Steve Jessop