سؤال

I am trying to compile tests for a library which uses C++11 using CMake. I have had success using UnitTest++ with C++11 on Ubuntu, however Mac OS X is giving me linker errors.

In CMake my linker flags and libraries for APPLE look like this:

SET(CMAKE_CXX_FLAGS "--stdlib=libc++ --std=c++11")
TARGET_LINK_LIBRARIES(mytest unittest++)

Compiling with this gives me linker errors such as:

Undefined symbols for architecture x86_64:
  "std::string::c_str() const", referenced from:
      UnitTest::MemoryOutStream::GetText() const in libunittest++.a(MemoryOutStream.o)
  "std::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::str() const", referenced from:
      UnitTest::MemoryOutStream::GetText() const in libunittest++.a(MemoryOutStream.o)
  "std::ostream::~std::ostream()", referenced from:
      construction vtable for std::ostream-in-UnitTest::MemoryOutStream in libunittest++.a(TestRunner.o)
      construction vtable for std::ostream-in-UnitTest::MemoryOutStream in libunittest++.a(Test.o)
....

Here is the full paste.

هل كانت مفيدة؟

المحلول

It looks like your copy of libunittest++.a was compiled with a different implementation of the standard C++ library. On OS X there are now two implementations of the standard C++ library: GNU's libstdc++, whose header files reside in /usr/include/c++/4.2.1 and Clang's libc++, whose header files are in /usr/lib/c++/v1/.

Your copy of libunittest++.a appears to have been compiled with GNU's libstdc++, so you can either fix the issue by using the same library for your program:

SET(CMAKE_CXX_FLAGS "-stdlib=libstdc++ -std=c++11")

Or re-compile libunittest++.a with using Clang's libc++.

Also see this question for more helpful information.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top