简体   繁体   English

CppUnit在Mac OS X上与Clang链接错误

[英]CppUnit Linking error with Clang on Mac OS X

I am trying to compile a test program using CppUnit. 我正在尝试使用CppUnit编译测试程序。 The problem is, with this sample code: 问题是,使用此示例代码:

//[...]

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

I am getting the linking error: 我收到链接错误:

  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)

I am using the -lcppunit flag when invoking clang. 我在调用clang时使用-lcppunit标志。 When I run the make file on my Linux machine, it compiles fine. 当我在我的Linux机器上运行make文件时,它编译得很好。

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

Are in my /usr/local/lib/. 在我的/ usr / local / lib /中。 I even tried installing to /usr/lib and the same linking error occurs. 我甚至尝试安装到/ usr / lib并发生相同的链接错误。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks very much! 非常感谢!

EDIT: I figured out the problem. 编辑:我发现了问题。 I am using libc++ since I am using std::shared_ptr in my project. 我正在使用libc ++,因为我在我的项目中使用std :: shared_ptr。 Problem is that I tried compiling CppUnit with libc++ but it throws linking errors. 问题是我尝试用libc ++编译CppUnit但它会引发链接错误。 It seems it must be compiled with libstdc++, which would require me to install Fink or Macports so I could install the latest version of gcc and libstdc++. 它似乎必须用libstdc ++编译,这需要我安装Fink或Macports所以我可以安装最新版本的gcc和libstdc ++。 I really hope to avoid this since there will be a whole mess trying to set it up. 我真的希望避免这种情况,因为试图设置它会有一团糟。 I really also hope to avoid using Boost for the shared_ptr. 我真的也希望避免在shared_ptr中使用Boost。

Is any of this possible? 这有可能吗? If not I'll probably give in and install MacPorts 如果不是,我可能会放弃并安装MacPorts

I had same issue. 我有同样的问题。 It's OK after setting "C++ Standard Library" as "libstdc++(GNU C++ standard library)". 将“C ++标准库”设置为“libstdc ++(GNU C ++标准库)”后即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM