且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-10 11:33:52

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


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

在10.8和早期版本中,默认选择libstdc ++,10.9为了确保与libMLi3兼容,我们需要手动选择libstdc ++。 b
$ b

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


使用Libc ++编译未定义的引用






编辑:经过一番调查后,似乎-mmacosx-version-min的默认libstd。如果min版本

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.

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: http://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.

Related post: Compiling with Clang using Libc++ undefined references


Edit: After some investigations it seems there is a link between the "-mmacosx-version-min" and the choice of the default libstd. If min version < 10.9, then the default libstd is equal to "libstdc++", else to "libc++". The long term solution is clearly to use "-stdlib=libc++"