[LCP]Dynamic libraries and plugins

Paul Gearon pag at PISoftware.com
Tue Jul 2 09:54:06 UTC 2002


On Mon, 24 Jun 2002, Joachim Bauernberger wrote:

> I have a also a question regarding dlopen and friends (and when it would
> make sense to use them)....
> Say I have a program which is executed often (maybe even several times a
> second) with many different options.
> Due to soooo many options would it make sense to put different parts of
> the code into seperate shared libs and call dlopen() by hand?
> It would make the size of the binary smaller and many features which are
> not needed are therefore never loaded no?
> I specially think about saving initialization time of a program...

You won't gain much from this approach.  Firstly, the size of your program
won't really change if you dynamically load by hand, as opposed to linking
against a .so library (it might get a few bytes bigger, but there won't be
any real change).

Then, as soon as you call dlopen on the file the whole thing will get
mapped into memory, just the same as if you let it be automatically loaded
with the program.  So there isn't any real savings there.  About the only
difference I can see is that you can delay the mapping of multiple
libraries until later in the process.  So if you had a LOT of libraries to
load then you'd see an effect.

The best reason for dlopen and dlsym is to load from libraries which may
or may not be there.  For instance, MPlayer (the multimedia video player)
has a look at the file type and then tries to load the appropriate shared
library, depending on what it found.  If it's DivX (MPEG-4/MP3)it loads
one library, if it's MPEG-2/AC3 it loads another library, and so on.
Then if it sees, say, an apple quicktime file it might find that the
required library isn't there and exit.  If these libraries were not loaded
with dlopen then the program wouldn't be able to start at all if it didn't
see the quicktime library, even though you might be wanting to watch a
DivX file and you don't care about quicktime.  Recently I wanted to watch
an obscure file format, and MPlayer couldn't show it.  All I had to do was
find the appropriate library file and put it where MPlayer could see it
and it played fine.

(yes, I know that many of the libraries for MPlayer are not .so files, but
the principle is the same, and MPlayer seemed like a good example to me)

Hope I've made sense here.  My brain hasn't kicked in yet this morning.

Regards,
Paul Gearon

Catapultam habeo. Nisi pecuniam omnem mihi dabis, ad caput tuum saxum
immane mittam.
(Translation from latin: "I have a catapult. Give me all the money,
or I will fling an enormous rock at your head.")






More information about the linuxCprogramming mailing list