Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange warnings from the linker (ld) [duplicate]

Tags:

xcode

macos

ld

We are building a Mac OSX application which is written mostly in Obj-C/Cocoa. The application then statically links with some 3rd party libraries, written in C/C++ and compiled by us (on a command line, using either MacPorts or the usual "./configure && make"; all are universal binaries).

The application is working perfectly, but ad compile time we are always getting these strange linker warnings:

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

This comes from a C/C++ library. We are linking with these libs:

  1. libphonenumber, which is the one causing 4 of the 5 warnings, apparently. Compiled by us via "cmake".
  2. boost (libboost_thread-mt), responsible of 1 warning. Compiled with MacPorts.
  3. ICU (libicudata, libicuuc, libicui18n), compiled with MacPorts.
  4. Protocol Buffers, compiled via "./configure && make".

Please note:

  1. The application is working perfectly despite the warnings, but we'd like to get rid of them as they are annoying.
  2. The solution proposed by xcode with boost : linker(Id) Warning about visibility settings doesn't work: "Symbols hidden by default" has always been "YES".
like image 415
ItalyPaleAle Avatar asked Mar 27 '12 17:03

ItalyPaleAle


2 Answers

The solution proposed by xcode with boost : linker(Id) Warning about visibility settings doesn't work: "Symbols hidden by default" has always been "YES".

This has less to do with being set to "YES", and more to do with being set to the same value across all projects. Libs/projects that depend on other libs need to have a likewise setting for "Symbols hidden by default" in order to link properly and free of errors/warnings.

I've run into this before, and a simple change in Xcode for all projects to ensure the settings match typically resolves the problem. Since it sounds like you're compiling on the command line as well, the -fvisibility argument to gcc is what you need to look at.

like image 156
inspector-g Avatar answered Oct 04 '22 18:10

inspector-g


tl:dr; use -fvisibility=hidden as a gcc and llvm compiler switch in everything you compile, including your dependent libraries, unless you have a reason not to.

A good introduction to the -fvisibility and -fvisibility-inline-hidden compilation flags is available on Apple's web site, as of this writing. The article also goes into some detail on the __attribute__((visibility("hidden"))) and __attribute__((visibility("default"))) declarations.

like image 33
johnwbyrd Avatar answered Oct 04 '22 19:10

johnwbyrd