1

这是我的生成文件:

#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)

当我运行这个makefile时,我得到了这样的错误:

g++ Money.o MoneyTest.o -o TestUnitaire Money.o: 在函数main': Money.cpp:(.text+0x3c): undefined reference toCppUnit::TestFactoryRegistry::getRegistry(std::basic_string, std::allocator > const&)' Money.cpp:(.text+0x78): 未定义引用到CppUnit::TextTestRunner::TextTestRunner(CppUnit::Outputter*)' Money.cpp:(.text+0x8c): undefined reference toCppUnit::TestRunner::addTest(CppUnit::Test*)' Money.cpp:(.text+0x98): 未定义对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): 未定义引用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): 未定义对CppUnit::TextTestRunner::~TextTestRunner()' Money.cpp:(.text+0x233): undefined reference toCppUnit::TextTestRunner::~TextTestRunner() 的引用

我的班级之间似乎没有联系。有什么问题?

4

1 回答 1

3

-lcppunit标志在 中不正确CFLAGS,这是您放置 C 编译器标志的位置。您正在 (a) 编译 C++ 程序,而不是 C 程序,并且 (b) 该-l标志是链接器标志,而不是编译器标志。此外,该CC变量包含 C 编译器。您应该将该CXX变量用于 C++ 编译器。您的 makefile 应该类似于:

#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)
于 2013-04-13T16:10:42.340 回答