“bad codegen, pointer diff in boost” error in 32-b

2019-02-23 09:58发布

问题:

Under Mac OS X 10.6 I am building a C++ shared library which links to boost 1.46. I am using the command line tools installed with Xcode 4.0.

The 64-bit build works fine. When building for 32-bit, I get the following error message upon linking:

ld: bad codegen, pointer diff in boost::detail::sp_counted_base::sp_counted_base()to global weak symbol vtable for boost::detail::sp_counted_basefor architecture i386

The only workaround I found is to use g++-4.0 for the 32-bit build. The other compilers I tried (g++-4.2, llvm-g++-4.2 and clang++) all produce the error above.

回答1:

The issue is most likely that you're statically linking together two libraries with different values for the default visibility (-fvisibility). You can either make sure to use the same visibility flags for both boost and your project, or use an explicit export symbol file.



回答2:

you're probably linking a library built with gcc 4.0, which has incompatible ABI with gcc 4.2 and clang. You should rebuild all libraries used with gcc4.2 or clang, using same compiler options as your main application (particular note to the option "C++ standard library type" aka STANDARD_C_PLUS_PLUS_LIBRARY_TYPE which should be set to "static" or "dynamic" consistently in all libraries). Another useful option is "symbols hidden by default" but beware that enabling that would hide nasty bugs that would rise if you pass C++ objects between libraries compiled with different options/compilers.



回答3:

For information: In XCode, visibility is set in Code Generation > Inline Method Hidden and Symbols Hidden by default

Setting those to NO fixes this issue.

This corresponds in effet to gcc -fvisibility-inlines-hidden and -fvisibility flags. You may uses those to only adjust your code's setting instead of messing with boost.



回答4:

The problem seems to be fixed in the linker that is installed with XCode 4.2. Upon linking I now get a warning instead of the error message:

ld: warning: direct access in __ZN5boost6detail15sp_counted_baseC2Ev to global weak symbol __ZTVN5boost6detail15sp_counted_baseE means the weak symbol cannot be overridden at runtime. This was likely caused by different translation units being compiled with different visibility settings.


回答5:

As said here:

If boost is included by multiple projects, each project must have the same values for

 Symbols Hidden by Default
 Inline Methods Hidden