Visibility linker warnings when compiling iOS app

2019-05-16 18:18发布

问题:

My iOS app uses a library which in turn depends on Boost. While revamping the 3rdparty build system, I encountered linker warnings such as this

ld: warning: direct access in ___cxx_global_var_init to global weak symbol
std::__1::basic_ofstream<char, std::__1::char_traits<char> >::~basic_ofstream()
means the weak symbol cannot be overridden at runtime.
This was likely caused by different translation units being compiled with different visibility settings.

and this

ld: warning: direct access in __GLOBAL__I_a to global weak symbol
boost::exception_ptr::~exception_ptr()
means the weak symbol cannot be overridden at runtime.
This was likely caused by different translation units being compiled with different visibility settings.

I saw that there are quite a few other questions on SO regarding this warning. I followed their advice and made sure that the visibility settings in all builds are the same (-fvisibility=hidden and -fvisibility-inlines-hidden). After a full rebuild I still got the warnings.

Environment:

  • Xcode 4.6.0
  • iOS SDK 6.1
  • Building Boost using a modified clone of the popular galbraithjosephs-boostoniphone build script
  • Boost version 1.54.0
  • Building with -std=c++11 -stdlib=libc++

回答1:

The problem was that the Boost build script used clang++ as the compiler, but the other library and my own iOS app used clang (the other library and my app are Xcode projects where builds are made with clang).

The warnings disappeared after I rebuilt Boost with clang.

The sad thing is that I don't know exactly why this helped. I am aware that compiling with clang++ switches the compiler into C++ mode, turning on/off certain settings. But it certainly can't mean that using clang++ overrides visibility settings that are explicitly specified on the command line?! Comments or additional answers explaining the behaviour are welcome.