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 ?
A .o file inside a library might contain symbols (functions, variables etc.) that are not used by your program. At link time, a static library can have unresolved symbols in it, as long as you don't need the unresolved symbols, and you don't need any symbol that is in a .o file that contains an unresolved symbol.
Static libraries are just collections of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime.
The sample projects in this article were created using Visual Studio 2010. A static library consists of object files that are linked together with an exe file. Object files are the output of compilers of unmanaged code and consists of functions that can only be used by unmanaged code.
A static library (also known as an archive) consists of routines that are compiled and linked directly into your program.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With