According to the doc, dlopen is used in conjunction with dlsym
to load a library, and get a pointer to a symbol.
But that's already what the dynamic loader/linker does. Moreover, both methods are based on ld.so.
There actually seems to be two differences when using dlopen
:
- The library can be conditionally loaded.
- The compiler is not aware of the symbols (types, prototypes...) we're using, and thus does not check for potential errors. It's, by the way, a way to achieve introspection.
But, it does not seem to motivate the use of dlopen
over standard loading, except for marginal examples:
- Conditional loading is not really interesting, in terms of memory footprint optimization, when the shared library is already used by another program: Loading an already used library is not increasing the memory footprint.
- Avoiding the compiler supervision is unsafe and a good way to write bugs... We're also missing the potential compiler optimizations.
So, are there other uses where dlopen
is prefered over the standard dynamic linking/loading?