26

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

2 回答 2

36

答案就在那里: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++ 添加到链接命令中。

相关文章:使用 Libc++ 未定义引用使用 Clang 进行编译


编辑: 经过一番调查后,似乎-mmacosx-version-min和默认选项之间存在联系libstd。如果 min version < 10.9,则默认libstd等于libstdc++,否则等于libc++。长期的解决方案显然是使用-stdlib=libc++

于 2013-10-28T14:18:15.970 回答
0

这些建议对我来说不适用于 Mac El capitan。如果您在升级到 El Capitan 后遇到类似问题,只需运行

xcode-select --install

在尝试编译之前

于 2016-06-03T06:19:18.133 回答