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

醉酒当歌 提交于 2019-12-11 00:46:22

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/22649406/how-to-link-in-std-c-library-on-mac-os-x-mavericks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!