What are the use cases of dlopen vs standard dynamic linking?

Viewed 3975

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:

  1. The library can be conditionally loaded.
  2. 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:

  1. 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.
  2. 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?

2 Answers
Related