Search code examples
c++shared-librariesstatic-librariessymbolsvisibility

hidding symbols from a static library


I have a c++ shared library - libA.so. I hide symbols using -fvisibility=hidden flag. The symbols which should be visible have the following attribute: __attribute__ ((visibility ("default"))). That library links to the static library - libB.a. All symbols from that static library (libB.a) should be visible only for the shared library (libA.so). I have compiled the static library also with the flag -fvisibility=hidden. I get desired results - symbols from libB.a are only visible for libA.so but are hidden for the outside world, for example the following command nm -C libA.so doesn't show symbols from libB.a.

Could you explain how the flag -fvisibility=hidden works internally with the static library in the above scenario ?


Solution

  • Visibility feature has been added to support limiting interfaces of shared libraries i.e. reducing number of exported functions. By default (without -fvisibility=hidden) linker will export all functions in library which is, in most cases, not what you want.

    Visibility is ignored during static library link i.e. when linking libB.a and pulling functions from it linker will not look at visibility annotations at all. On the other hand, after functions are pulled (from libB.a) into final shared library (libA.so) they will be exported and pollute libA's interface unless you've hidden them via -fvisibility=hidden when compiling the static library itself.