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

2

3

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.

Classicist answered 26/3, 2014 at 0:54 Comment(2)
I think you have to invoke the C++ compiler and linker by using the clang++ command instead. The clang executable is the C compiler.Sandpaper
I had a similar issue on Linux trying to build one project with clang and libc++. That project had a binary linked with a third-party dynamic lib built with g++ and linked with libstdc++ (installed via package manager). Once I got rid of that dependency, the problem disappeared.Luzern
C
6

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.

Classicist answered 26/3, 2014 at 2:16 Comment(1)
@Rico Is that better now? This does solve my problem and was not provided in the other answer, the key was adding -lc++, the adding of -std=c++11 -stdlib=libc++ were already in my link line, so the other answer did not help.Classicist
S
1

Using the CLang++ compiler on my MacBook Pro OS X Mavericks 9.2, within NetBeans 7.4, I have

-std=c++11 -stdlib=libc++ -Wall

I am sure that I obtained the libc++ from installing the latest Xcode Command Line Tools for Mavericks. On my system, the dynamic libc++ libraries are located in the /usr/lib directory.


Start Edit
I have just tried a basic, "Hello, World," run from Xcode, making sure the LLVM 5.0 (CLang++) compiler settings within the .xcodeproj are configured for:

C++ Language Dialect        C++11 [-std=c++11]
C++ Standard Library        libc++ (LLVM C++ Standard library with C++11 support)

C Language Dialect          c11


All works as expected.
End Edit

Sandpaper answered 26/3, 2014 at 1:9 Comment(1)
Using -std=c++11 -stdlib=libc++ on the link line gives the same missing symbols. See edited question.Classicist

© 2022 - 2024 — McMap. All rights reserved.