Symbol visibility using g++

JoeSlav picture JoeSlav · Feb 14, 2011 · Viewed 11.2k times · Source

I compiled a C++ library under Linux/Mac with its symbols hidden. I've used _attribute_ ((visibility("hidden"))) for all my classes and compiled with options (-c -O2 -fPIC -MMD -MP -MF). Under Mac, using MacDependencies (http://code.google.com/p/macdependency/), the job is done just fine as I see only my exports (I actually saw the difference before and after).

However, I noticed that using nm I still see all the names of the symbols. This happens under both Mac and Linux.

Why is that? Is there any way to avoid this?

Best Regards and thanks, Joe

Answer

Maxim Egorushkin picture Maxim Egorushkin · Feb 14, 2011

Public or hidden, symbols are still there. nm shows all symbols. The difference is that hidden symbols are not available to the dynamic linker, i.e. not exported and can not be interposed.

You might also like the following man gcc:

   -fvisibility=default|internal|hidden|protected
       ...
       A good explanation of the benefits offered by ensuring ELF symbols
       have the correct visibility is given by "How To Write Shared
       Libraries" by Ulrich Drepper (which can be found at
       <http://people.redhat.com/~drepper/>)---however a superior solution
       made possible by this option to marking things hidden when the
       default is public is to make the default hidden and mark things
       public.  This is the norm with DLL's on Windows and with
       -fvisibility=hidden and "__attribute__ ((visibility("default")))"
       instead of "__declspec(dllexport)" you get almost identical
       semantics with identical syntax.  This is a great boon to those
       working with cross-platform projects.