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

时间:2013-10-28 14:17:06

标签: c++ linker clang++ osx-mavericks

升级到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

2 个答案:

答案 0 :(得分:35)

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

  

OS X上有两种标准C ++库实现:libstdc ++和libc ++。它们不是二进制兼容的,libMLi3需要libstdc ++。

     

10.8和更早版本默认情况下选择libstdc ++,默认情况下选择10.9 libc ++。为确保与libMLi3的兼容性,我们需要手动选择libstdc ++。

     

为此,请将-stdlib = libstdc ++添加到链接命令。

相关文章:Compiling with Clang using Libc++ undefined references


修改:经过一些调查后,似乎-mmacosx-version-min与默认libstd的选择之间存在关联。如果最小版本&lt; 10.9,然后默认libstd等于libstdc++,否则等于libc++。长期解决方案显然是使用-stdlib=libc++

答案 1 :(得分:1)

这些建议对Mac El capitan不起作用。如果您在升级到El Capitan后遇到类似问题,请运行

xcode-select --install

在尝试编译之前