Reputation: 765
Question says it all really. This is more for my own curiosity to see if anyone knows of a particular reason why this should be avoided!
The only thought I have had is that perhaps, even if no library functions are called, the compiler will still generate loading code and load the library even though it will not be used. I'm guessing here, would be interested to know what the implications actually are!
Thanks!
Upvotes: 0
Views: 208
Reputation: 21955
At least on Linux, whenever loader loads a library, it'll spend some time resolving it's dynamic relocations (hit 1). Also having unused libraries sitting in memory will slow down symbol resolution for subsequently loaded libs (hit 2). And finally, loader will run initializers for all loaded libraries (typically C++ constructors but often other stuff) which will consume time (hit 3) and may also effectively disable lazy loading (as initializer calls functions defined in library thus forcing them to be resolved by loader) (hit 4).
As a side note - modern Linux toolchains have a nice flag -Wl,--as-needed
which will automatically get rid of unused dynamic dependencies (it's not always straightforward due to transitive dependencies but result is worth it).
Upvotes: 1
Reputation: 2298
At program start up you should expect that the library will be loaded. This is a memory usage and start up time impact (I am not saying how small/big an impact). This is the standard case on Windows with the Microsoft tool chain and, as I understand this answer to another question, is also the case on Linux.
Note that this is different than using LoadLibrary on Windows. LoadLibrary is a Windows facility to explicitly load and use a dynamic library at run time without linking at build time.
The Microsoft tool chain does allow you to optionally specify that a Dll is to be delay loaded. If you do this, then the dll will not actually be loaded until/unless you use the dll. The tool chain does this by substituting build time dll linking with run time loading on demand.
Upvotes: 1
Reputation: 1869
If you don't call LoadLibrary, or automatically delay-load the DLL, there are no pitfalls.
https://msdn.microsoft.com/en-us/library/hf3f62bz.aspx
If the library is actually loaded, DllMain will be called. If the DllMain contains bugs or malicious code, anything can happen.
Upvotes: 0