Static libraries are basically archives of object files. I guess that made sense at some point in the prehistoric times? But this means that you can't control symbol visibility, and global constructors/destructors are broken, and I'm troubled by that.
Say I have a function internal_foo(), that is used only within
the implementation of my library, but across different translation units.
Dunno about you, but I experience a profound sense of unease at the realization that there's no
way to hide this symbol from the consumers of my static library.
With shared libraries you can at least use -fvisibility=hidden and stuff.
By the way, you should compile with -fvisibility=hidden always, even for static libraries.
This enables more optimizations and inlining for these functions, because the compiler knows that no
dynamic interposition will happen.
Also, since static libraries are just bundles of object files, the linker will only
take those object files which it needs for symbols required elsewhere.
So, constructors of global C++ variables won't get called, unless the object file
containing that variable is needed in some other way.
It's possible to use -Wl,--whole-archive to circumvent this, but eh.
Shared libraries don't suffer from all this, because they are a proper format, not just a mindless archive containing object files. Unfortunately, we can't link shared libraries statically, because all the relocations have already been applied and that breaks the linker or something idk.
All I'm saying is that I wish we had better way to package static libraries. Surely it's not that hard?..