这是我的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)当我运行这个makefile时,会得到这样的错误:
O MoneyTest.o
main': Money.cpp:(.text+0x3c): undefined reference toCppUnit::TestFactoryRegistry::getRegistry(std::basic_string,-o TestUnitaire Money.o:在函数g++ 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,CppUnit::TextTestRunner::setOutputter(CppUnit::Outputter*)' Money.cpp:(.text+0x168): undefined reference toCppUnit::TextTestRunner::run(std::basic_string,::分配器> const&)‘Money.cpp:(.text+0xfc):未定义的对CppUnit::TextTestRunner::~TextTestRunner()' Money.cpp:(.text+0x233): undefined reference toCppUnit::TextTestRunner::~TextTestRunner()’的引用::分配程序>,bool,bool,bool)‘Money.cpp:(.text+0x1a5):未定义的对CppUnit::TextTestRunner::~TextTestRunner()' Money.cpp:(.text+0x233): undefined reference toCppUnit::TextTestRunner::~TextTestRunner()’的引用
似乎我们班之间没有任何联系。有什么问题吗?
发布于 2013-04-13 16:10:42
-lcppunit标志在CFLAGS中是不正确的,这是放置C编译器标志的地方。您是(a)编译C++程序,而不是C程序,和(b) -l标志是链接器标志,而不是编译器标志。此外,CC变量包含C编译器。您应该将CXX变量用于C++编译器。生成文件应该如下所示:
#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)https://stackoverflow.com/questions/15989714
复制相似问题