10

既然 Mac OS X 上可以存在多个 C++ 标准库,现在看起来情况相当混乱。根据https://stackoverflow.com/a/8457799/1772681的说法,将 libstdc++ 和 libc++ 混合会导致链接错误,这会遇到这种危险情况,这是一件好事。

另一方面,还有 2 种情况需要进一步调查,我在 github gist ( https://gist.github.com/manphiz/7195515 ) 中为此创建了一些测试用例。它确认混合链接到 libstdc++(来自系统或 vanilla GNU GCC)和 libc++(系统)的动态库将导致链接错误。但是,如果一个动态库链接到系统 libstdc++,而另一个动态库链接到 vanilla GNU GCC libstdc++,然后将两者链接到二进制文件中也可以工作,对于我的简单测试用例,它甚至可以在运行时工作。

$ make -f Makefile.system_gnu 
g++-4.8 -g -Wall -fPIC -o main.o -c main.cc
g++-4.8 -g -Wall -fPIC -o test_a.o -c test_a.cc
g++-4.8 -dynamiclib -o libtest_a.dylib test_a.o
clang++ -g -Wall -fPIC "-stdlib=libstdc++" -o test_b.o -c test_b.cc
clang++ -dynamiclib "-stdlib=libstdc++" -o libtest_b.dylib test_b.o
g++-4.8 -o test main.o -L. -ltest_a -ltest_b

$ ./test
main_test_a_test_b

所以这里需要建议:

  • 我们可以混合使用系统 libstdc++ 和手动构建的 GNU GCC libstdc++ 吗?如果没有,什么时候会造成麻烦?
  • 我们可以混合使用系统 libc++ 和手动构建的 Clang libc++ 吗?如果没有,什么时候会造成麻烦?

编译器信息:

$ clang -v
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix

$ gcc-4.8 -v
Using built-in specs.
COLLECT_GCC=gcc-4.8
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc48/4.8.2/libexec/gcc/x86_64-apple-darwin13.0.0/4.8.2/lto-wrapper
Target: x86_64-apple-darwin13.0.0
Configured with: ../configure --build=x86_64-apple-darwin13.0.0 --prefix=/opt/homebrew/Cellar/gcc48/4.8.2 --enable-languages=c,c++,fortran,java,objc,obj-c++ --program-suffix=-4.8 --with-gmp=/opt/homebrew/opt/gmp4 --with-mpfr=/opt/homebrew/opt/mpfr2 --with-mpc=/opt/homebrew/opt/libmpc08 --with-cloog=/opt/homebrew/opt/cloog018 --with-isl=/opt/homebrew/opt/isl011 --with-system-zlib --enable-version-specific-runtime-libs --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --enable-plugin --disable-nls --with-ecj-jar=/opt/homebrew/opt/ecj/share/java/ecj.jar --enable-multilib
Thread model: posix
gcc version 4.8.2 (GCC)

系统为 Mac OS X 10.9。

4

1 回答 1

5

我不代表 Apple,但看着他们的行动,我相信他们的目标是回到 Mac OS(和 iOS)的一个标准库实现——那就是 libc++。我相信在未来的某个时候,libstdc++ 将不再是 Mac OS X 的一部分。

我们可以混合使用系统 libc++ 和手动构建的 Clang libc++ 吗?如果没有,什么时候会造成麻烦?

我经常这样做——但我不会替换 usr/lib 中的那个。相反,我在设置环境变量 DYLD_LIBRARY_PATH 以指向我新建的 libc++ 后运行特定程序。替换 /usr/lib 中的那个可能会使您的系统变砖。(如果您破坏了 dylib 中的某些内容 - 或者甚至只是更改 的布局std::string,例如)。

于 2013-10-28T19:39:49.390 回答