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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19637164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:01:04  来源:igfitidea点击:

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

c++linkerclang++osx-mavericks

提问by FabienRohrer

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

回答by FabienRohrer

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++. They are not binary compatible and libMLi3 requires libstdc++.

On 10.8 and earlier libstdc++ is chosen by default, on 10.9 libc++ is chosen by default. To ensure compatibility with libMLi3, we need to choose libstdc++ manually.

To do this, add -stdlib=libstdc++ to the linking command.

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

在 10.8 及更早版本上默认选择 libstdc++,在 10.9 上默认选择 libc++。为了保证与libMLi3的兼容性,我们需要手动选择libstdc++。

为此,请将 -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-minand the choice of the default libstd. If min version < 10.9, then the default libstdis equal to libstdc++, else to libc++. The long term solution is clearly to use -stdlib=libc++

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

回答by Omer Cansizoglu

Those suggestions did not work for me with Mac El capitan. If you have similar issues after upgrading to El Capitan, just run

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

xcode-select --install

before trying to compile

在尝试编译之前