2012-12-26 42 views
7

Tôi đang cố gắng biên dịch một chương trình thử nghiệm bằng CppUnit. Vấn đề là, với mẫu mã này:Lỗi liên kết CppUnit với Clang trên Mac OS X

//[...] 

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; 
} 

Tôi nhận được lỗi khi liên kết:

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) 

Tôi đang sử dụng lá cờ -lcppunit khi gọi kêu vang. Khi tôi chạy tập tin trên máy Linux của tôi, nó biên dịch tốt.

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

Có trong/usr/local/lib /. Tôi thậm chí đã thử cài đặt vào/usr/lib và cùng một lỗi liên kết xảy ra. Mọi sự trợ giúp sẽ rất được trân trọng.

Cảm ơn rất nhiều!

EDIT: Tôi đã tìm ra sự cố. Tôi đang sử dụng libc + + kể từ khi tôi đang sử dụng std :: shared_ptr trong dự án của tôi. Vấn đề là tôi đã thử biên dịch CppUnit với libC++ nhưng nó ném các lỗi liên kết. Có vẻ như nó phải được biên dịch với libstdC++, mà sẽ yêu cầu tôi cài đặt Fink hoặc Macports để tôi có thể cài đặt phiên bản mới nhất của gcc và libstdC++. Tôi thực sự hy vọng để tránh điều này vì sẽ có một mớ hỗn độn cố gắng để thiết lập nó. Tôi thực sự cũng hy vọng tránh sử dụng Boost cho shared_ptr.

Có phải điều này có thể xảy ra không? Nếu không, tôi có thể sẽ không cung cấp và cài đặt MacPorts

+0

Khi bạn giải quyết vấn đề của riêng mình, hãy đăng giải pháp của bạn làm câu trả lời. –

Trả lời

1

Tôi gặp vấn đề tương tự. OK sau khi thiết lập "Thư viện chuẩn C++" là "libstdC++ (thư viện chuẩn GNU C++)".