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 to
CppUnit::TestFactoryRegistry::getRegistry(std::basic_string, std::allocator > const&)' Money.cpp:(.text+0x78): undefined reference toCppUnit::TextTestRunner::TextTestRunner(CppUnit::Outputter*)' Money.cpp:(.text+0x8c): undefined reference to
CppUnit::TestRunner::addTest(CppUnit::Test*)' Money.cpp:(.text+0x98): undefined reference toCppUnit::TextTestRunner::result() const' Money.cpp:(.text+0xec): undefined reference to
CppUnit::CompilerOutputter::CompilerOutputter(CppUnit::TestResultCollector*, std::basic_ostream >&, std::basic_string, std::allocator > const&)' Money.cpp:(.text+0xfc): undefined reference toCppUnit::TextTestRunner::setOutputter(CppUnit::Outputter*)' Money.cpp:(.text+0x168): undefined reference to
CppUnit::TextTestRunner::run(std::basic_string, std::allocator >, bool, bool, bool)' Money.cpp:(.text+0x1a5): undefined reference toCppUnit::TextTestRunner::~TextTestRunner()' Money.cpp:(.text+0x233): undefined reference to
CppUnit::TextTestRunner::~TextTestRunner()'
It's seems to be that there no link between my class. What's the trouble ?
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)