Search code examples
c++hyperlinkclangosx-maverickslibstdc++

How to link in std C++ library on Mac OS X Mavericks?


I'm porting an application to OS X Darwin and am getting link errors with missing symbols like:

std::__1::basic_string<char, std::__1::char_traits<char>,
                       std::__1::allocator<char> >::find_last_of(char const*,
                                                                 unsigned long,
                                                                 unsigned long) const
operator delete[](void*)
typeinfo for std::runtime_error
std::set_unexpected(void (*)())
std::exception::~exception()
[...]

I expect these should come from libstdc++ but I don't see how to link that in using clang.

Here is my attempted link line and the resulting failure:

clang -std=c++11 -stdlib=libc++ -m64 -o ARCH.darwin_1310_i86/release/myExec ARCH.darwin_1310_i86/release/myExec.o ../../src/netcomm/ARCH.darwin_1310_i86/release/libmyExec.a ../../src/common/ARCH.darwin_1310_i86/release/libcommon.a -L../zlib  -lz -L../Botan -lbotan-1.10 -lboost_thread-mt
Undefined symbols for architecture x86_64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::find_last_of(char const*, unsigned long, unsigned long) const", referenced from:
[...]

But this did not work, I'm not finding any examples of how to link it in correctly.


Solution

  • You need to add -lc++ to the link line like this:

    clang -std=c++11 -stdlib=libc++ -lc++ -m64 -o ARCH.darwin_1310_i86/release/myExec ARCH.darwin_1310_i86/release/myExec.o ../../src/netcomm/ARCH.darwin_1310_i86/release/libmyExec.a ../../src/common/ARCH.darwin_1310_i86/release/libcommon.a -L../zlib -lz -L../Botan -lbotan-1.10 -lboost_thread-mt

    After adding that, the missing symbols go away.