2012-12-26 75 views
7

我正在嘗試使用CppUnit編譯測試程序。問題是,這個示例代碼:CppUnit在Mac OS X上與Clang鏈接錯誤

//[...] 

class EntityComponentTest : public CppUnit::TestFixture 
{ 
CPPUNIT_TEST_SUITE(EntityComponentTest); 
CPPUNIT_TEST(testGetComponents); 
CPPUNIT_TEST_SUITE_END(); 
Entity e; 


public: 
void setUp(){ 
    e.addComponent("1", new TestComponent("Hello 1")); 
    e.addComponent("2", new TestComponent("Hello 2")); 
} 

void tearDown(){} 

void testGetComponents() 
{ 
    TestComponent &first = static_cast<TestComponent&>(e.getComponent("1")); 
    TestComponent &second = static_cast<TestComponent&>(e.getComponent("2")); 

    CPPUNIT_ASSERT(first.msg == "Hello 1"); 
    CPPUNIT_ASSERT(second.msg == "Hello 2"); 

} 


}; 
CPPUNIT_TEST_SUITE_REGISTRATION(EntityComponentTest); 
int main(void) 
{ 
//followed from tutorial 
CppUnit::TextUi::TestRunner run; 
CppUnit::TestFactoryRegistry &r = CppUnit::TestFactoryRegistry::getRegistry(); 
run.addTest(r.makeTest()); 

run.run("", false, true); 

return 0; 
} 

我收到鏈接錯誤:

Undefined symbols for architecture x86_64: 
    "CppUnit::SourceLine::SourceLine(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from: 
    EntityComponentTest::testGetComponents() in EntityComponentTest.cpp.o 
    "CppUnit::TextTestRunner::run(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, bool, bool)", referenced from: 
    _main in EntityComponentTest.cpp.o 
    "CppUnit::TestFactoryRegistry::getRegistry(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from: 
    _main in EntityComponentTest.cpp.o 
    CppUnit::AutoRegisterSuite<EntityComponentTest>::AutoRegisterSuite() in EntityComponentTest.cpp.o 
    "CppUnit::Message::Message(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from: 
    EntityComponentTest::testGetComponents() in EntityComponentTest.cpp.o 
"CppUnit::TestCase::TestCase(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from: 
    CppUnit::TestCaller<EntityComponentTest>::TestCaller(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void (EntityComponentTest::*)(), EntityComponentTest*) in EntityComponentTest.cpp.o 
"CppUnit::TestSuite::TestSuite(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from: 
    EntityComponentTest::suite() in EntityComponentTest.cpp.o 
"CppUnit::TestSuiteBuilderContextBase::getTestNameFor(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from: 
    EntityComponentTest::addTestsToSuite(CppUnit::TestSuiteBuilderContextBase&) in EntityComponentTest.cpp.o 
"CppUnit::Test::findTestPath(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, CppUnit::TestPath&) const", referenced from: 
    vtable for CppUnit::TestCaller<EntityComponentTest> in EntityComponentTest.cpp.o 
"CppUnit::Test::resolveTestPath(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from: 
    vtable for CppUnit::TestCaller<EntityComponentTest> in EntityComponentTest.cpp.o 
"CppUnit::Test::findTest(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from: 
    vtable for CppUnit::TestCaller<EntityComponentTest> in EntityComponentTest.cpp.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

調用鐺當我使用-lcppunit標誌。當我在我的Linux機器上運行make文件時,它編譯得很好。

libcppunit-1.12.1.0.0.dylib 
libcppunit-1.12.1.dylib 
libcppunit.a 
libcppunit.dylib 

在我的/ usr/local/lib /中。我甚至嘗試安裝到/ usr/lib併發生相同的鏈接錯誤。任何幫助將不勝感激。

非常感謝!

編輯:我想通了這個問題。我正在使用libC++,因爲我在我的項目中使用了std :: shared_ptr。問題是我試圖用libC++編譯CppUnit,但它會引發鏈接錯誤。看來它必須用libstdC++編譯,這需要我安裝Fink或Macports,這樣我才能安裝最新版本的gcc和libstdC++。我真的希望能夠避免這種情況,因爲嘗試設置它會導致一團糟。我真的也希望避免爲shared_ptr使用Boost。

這是可能的嗎?如果沒有,我可能會放棄並安裝MacPorts

+0

當您解決自己的問題時,請將您的解決方案作爲答案。 –

回答

1

我有同樣的問題。將「C++標準庫」設置爲「libstdC++(GNU C++標準庫)」後可以。