Search code examples
makefilecppunit

makefile for cppunit


this is my makefile :

#Makefile
CC=g++
CFLAGS=-lcppunit
OBJS=Money.o MoneyTest.o

all : $(OBJS)
    $(CC) $(OBJS) -o TestUnitaire

#création des objets 
Money.o: Money.cpp Money.hpp
    $(CC) -c Money.cpp $(CFLAGS)

MoneyTest.o: MoneyTest.cpp Money.hpp MoneyTest.hpp
    $(CC) -c MoneyTest.cpp $(CFLAGS)

clean:
    rm *.o $(EXEC)

when i run this makefile, i get errors like those :

g++ Money.o MoneyTest.o -o TestUnitaire Money.o: In function main': Money.cpp:(.text+0x3c): undefined reference toCppUnit::TestFactoryRegistry::getRegistry(std::basic_string, std::allocator > const&)' Money.cpp:(.text+0x78): undefined reference to CppUnit::TextTestRunner::TextTestRunner(CppUnit::Outputter*)' Money.cpp:(.text+0x8c): undefined reference toCppUnit::TestRunner::addTest(CppUnit::Test*)' Money.cpp:(.text+0x98): undefined reference to CppUnit::TextTestRunner::result() const' Money.cpp:(.text+0xec): undefined reference toCppUnit::CompilerOutputter::CompilerOutputter(CppUnit::TestResultCollector*, std::basic_ostream >&, std::basic_string, std::allocator > const&)' Money.cpp:(.text+0xfc): undefined reference to CppUnit::TextTestRunner::setOutputter(CppUnit::Outputter*)' Money.cpp:(.text+0x168): undefined reference toCppUnit::TextTestRunner::run(std::basic_string, std::allocator >, bool, bool, bool)' Money.cpp:(.text+0x1a5): undefined reference to CppUnit::TextTestRunner::~TextTestRunner()' Money.cpp:(.text+0x233): undefined reference toCppUnit::TextTestRunner::~TextTestRunner()'

It's seems to be that there no link between my class. What's the trouble ?


Solution

  • The -lcppunit flag is not correct in CFLAGS, which is where you put C compiler flags. You are (a) compiling C++ programs, not C programs, and (b) the -l flag is a linker flag, not a compiler flag. Also, the CC variable holds the C compiler. You should use the CXX variable for the C++ compiler. Your makefile should look something like:

    #Makefile
    CXX = g++
    LDLIBS = -lcppunit
    OBJS = Money.o MoneyTest.o
    
    all : TestUnitaire
    
    TestUnitaire: $(OBJS)
            $(CXX) $^ -o $@ $(LDFLAGS) $(LDLIBS)
    
    #création des objets
    %.o : %.cpp
            $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<
    
    Money.o: Money.hpp
    MoneyTest.o: Money.hpp MoneyTest.hpp
    
    clean:
            rm *.o $(EXEC)