Yes. The dlopen(3) linux man page says:
If the same library is loaded again with dlopen(), the same file
handle is returned. The dl library maintains reference counts for
library handles, so a dynamic library is not deallocated until
dlclose() has been called on it as many times as dlopen()
has succeeded on it.
BTW, on Linux systems, you can dlopen a lot (many dozens of thousands) of shared libraries, as my example manydl.c demonstrates. The main limitation is address space.
So practically, not bothering about dlclose
-ing stuff is possible.
(unless your dlopen-ed shared libraries have weird or resource consuming constructor or destructor functions)
Added in December 2017:
Notice that what is relevant is the exact path string passed to dlopen
. So if you use "./foo.so"
and "././foo.so"
(or "../foosymlink.so"
where foosymlink.so
is a symlink to foo.so
) the dlopen-ed handles are different, and in some cases weird behavior of the two instances of that shared library might happen.
added in june 2019:
Read also Drepper's How to write shared libraries paper (it explains also well how to use them!).