Search code examples
dllshared-librariesdynamic-linkingdllexport

Are there technical reasons for hiding symbols in a shared library?


What's the purpose of hiding symbols in a dll as opposed to just exporting everything? Other than not taking up a little more space in the exe is there any other benefit?


Solution

  • is there any other benefit?

    There are lots of other benefits.

    For the library itself, hidden symbols can not be interposed. When you have code like this in a shared library:

    int foo() { return 20; }
    int bar() { return foo() + 1; }
    

    what is the return from bar? You might think it's 21, but you may be wrong: if an earlier library defined its own foo, the return from bar could be anything.

    Other benefits of symbol hiding include faster symbol resolution time (when the loader looks up foo, it has to search each loaded library in turn, and that goes faster if the exported set of functions is small), and smaller RAM requirements (since the symbol names are needed by the dynamic loader, they are all present in memory).

    Symbol hiding also prevents end users from accidentally depending on library implementation details, allowing you to change the implementation without risk of breaking existing users.