2

Not sure what issue I am having here.

  • Mac clang 3.1 cmake
  • gtest
  • few files
  • using few C++11 features
  • I have downloaded and installed XCode build tools

CMAKE_CXX_FLAGS = -Wall -std=c++0x -stdlib=libc++ -v

builds fine with output...

[100%] Building CXX object CMakeFiles/soupbintcptest.dir/soupmessages_tests.cpp.o
clang version 3.1 (tags/RELEASE_31/final)
Target: x86_64-apple-darwin11.3.0
Thread model: posix

Linking dumps a slew of errors. ABI errors? I know there is this C++11 namespace mangling thing happening.

Undefined symbols for architecture x86_64:

They are all things in the std:: namespace that will not link like:

 "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setprecision)", referenced from:
      testing::AssertionResult testing::internal::FloatingPointLE<float>(char const*, char const*, float, float) in libgtest.a(gtest-all.cc.o)
      testing::AssertionResult testing::internal::FloatingPointLE<double>(char const*, char const*, double, double) in libgtest.a(gtest-all.cc.o)
      testing::Message::Message() in libgtest.a(gtest-all.cc.o)

or

"std::cerr", referenced from:
      testing::internal::GTestLog::GetStream() in libgtest.a(gtest-all.cc.o)
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
      testing::UnitTest::AddTestPartResult(testing::TestPartResult::Type, char const*, int, testing::internal::String const&, testing::internal::String const&) in libgtest.a(gtest-all.cc.o)
      testing::internal::SingleFailureChecker::~SingleFailureChecker() in libgtest.a(gtest-all.cc.o)
      testing::internal::StringStreamToString(std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*) in libgtest.a(gtest-all.cc.o)
      testing::TestInfo::TestInfo(char const*, char const*, char const*, char const*, void const*, testing::internal::TestFactoryBase*) in libgtest.a(gtest-all.cc.o)
      testing::TestInfo::~TestInfo() in libgtest.a(gtest-all.cc.o)
      testing::internal::ReportInvalidTestCaseType(char const*, char const*, int) in libgtest.a(gtest-all.cc.o)
      testing::internal::XmlUnitTestResultPrinter::PrintXmlUnitTest(__sFILE*, testing::UnitTest const&) in libgtest.a(gtest-all.cc.o)
      ...

I believe I have built gtest with the same clang++ version. Not sure what else is going on here to drive these issues.

Craig Scott
  • 9,238
  • 5
  • 56
  • 85
user1460739
  • 319
  • 2
  • 8

2 Answers2

1

I've seen similar errors to yours when the linker is pulling in a different c++ library to the one that matches what the compiler is building for. In your case, you are only modifying the compiler flags, but the linker doesn't know your choice and is likely linking to the wrong C++ library.

If you are able to use CMake 3.2 or later, then rather than modifying the C++ compiler flags directly, I'd recommend you just tell CMake you want to use C++11 and then let it work out the appropriate compiler and linker flags for you. This can be done by adding the following before your project() call:

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

This and related CMake features are discussed in detail in this article.

Craig Scott
  • 9,238
  • 5
  • 56
  • 85
  • There is also [another article on that blog](http://crascit.com/2015/07/25/cmake-gtest/) which deals explicitly with adding gtest to a CMake build which you may find useful. The approach discussed there is more robust to compiler/linker flag issues across different platforms. It builds gtest as part of your main build directly rather than requiring a pre-built one to be present. – Craig Scott Jan 24 '16 at 02:33
-1

There is nothing C++11 about name mangling, it's been going on since the very first C++ implementations. However, do not forget that you must have built gtest for the same stdlib switch, because it can't find the Standard libraries.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Sorry - meant [c++ 11 inline namespace](http://stackoverflow.com/questions/11016220/what-are-inline-namespaces-for) – user1460739 Jun 30 '12 at 11:35
  • I also just rebuilt gtest with same stdlib flags (which I see in my cmake for gtest were already that way anyhow) and have the same result. – user1460739 Jun 30 '12 at 11:42