简体   繁体   English

升级到Mac OS X 10.9 / Xcode 5.0.1后,C ++链接错误

[英]C++ linking error after upgrading to Mac OS X 10.9 / Xcode 5.0.1

After upgrading to Mac OS X 10.9 / Xcode 5.0.1, command lines to create a shared library (.dylib) failed with several undefined symbols. 升级到Mac OS X 10.9 / Xcode 5.0.1后,创建共享库(.dylib)的命令行失败,出现了几个未定义的符号。

clang++ -dynamiclib -install_name test.dylib *.o -o test.dylib
Undefined symbols for architecture x86_64:
  "std::allocator<char>::allocator()", referenced from:
      _main in test.o
  "std::allocator<char>::~allocator()", referenced from:
      _main in test.o
  "std::ostream::operator<<(std::ostream& (*)(std::ostream&))", referenced from:
      _main in test.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:
      _main in test.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
      _main in test.o
  "std::ios_base::Init::Init()", referenced from:
      ___cxx_global_var_init in test.o
  "std::ios_base::Init::~Init()", referenced from:
      ___cxx_global_var_init in test.o
  "std::cout", referenced from:
      _main in test.o
  "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
      _main in test.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<<<char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
      _main in test.o
ld: symbol(s) not found for architecture x86_64

The answer is there: https://mathematica.stackexchange.com/questions/34692/mathlink-linking-error-after-os-x-10-9-mavericks-upgrade 答案是: https//mathematica.stackexchange.com/questions/34692/mathlink-linking-error-after-os-x-10-9-mavericks-upgrade

There are two implementations of the standard C++ library available on OS X: libstdc++ and libc++. OS X上有两种标准C ++库实现:libstdc ++和libc ++。 They are not binary compatible and libMLi3 requires libstdc++. 它们不是二进制兼容的,libMLi3需要libstdc ++。

On 10.8 and earlier libstdc++ is chosen by default, on 10.9 libc++ is chosen by default. 在10.8及更早版本中,默认选择libstdc ++,默认情况下选择10.9 libc ++。 To ensure compatibility with libMLi3, we need to choose libstdc++ manually. 为确保与libMLi3的兼容性,我们需要手动选择libstdc ++。

To do this, add -stdlib=libstdc++ to the linking command. 为此,请将-stdlib = libstdc ++添加到链接命令。

Related post: Compiling with Clang using Libc++ undefined references 相关文章: 使用Libc ++未定义引用与Clang进行编译


Edit : After some investigations it seems there is a link between the -mmacosx-version-min and the choice of the default libstd . 编辑 :经过一些调查后,似乎-mmacosx-version-min与默认libstd的选择之间存在联系。 If min version < 10.9, then the default libstd is equal to libstdc++ , else to libc++ . 如果min version <10.9,那么默认的libstd等于libstdc++ ,否则等于libc++ The long term solution is clearly to use -stdlib=libc++ 长期解决方案显然是使用-stdlib=libc++

Those suggestions did not work for me with Mac El capitan. 这些建议对Mac El capitan不起作用。 If you have similar issues after upgrading to El Capitan, just run 如果您在升级到El Capitan后遇到类似问题,请运行

xcode-select --install

before trying to compile 在尝试编译之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM