Subject: Re: lib question
To: None <thebush@primenet.com>
From: Bill Studenmund <wrstuden@loki.stanford.edu>
List: port-mac68k
Date: 07/13/1998 21:07:51
> Whats the difference between static and shared libraries.

Shared libraries are where the code in the library is shared between all
programs using it. They're also called dynamic libraries because they
are linked in at run-time. Static libraries are linked in at compile
time.

Here's an example:

Say you have a program which uses printf. That's in libc. When you have
ld link your program, it sees your .o files want the printf routine.
It finds one in libc. If you're making a staticly-linked program (the
only thing you can do with static libraries), the printf routine
gets stuffed in your final executable.

If there's a dynamic libc around, and you haven't disabled dynamic linking,
then instead ld can put in hooks for the printf routine in libc. When the
loader loads your program, it will load in a copy of libc, and link
your calls to printf to the routine in libc.

The difference is that the copy of printf (and all the other routines in
libc you use) didn't get stuffed into your program. If you have 1000
programs, each using the same 4k worth of routines, you can save about
4000k with dynamic libraries. Also, all the currently-loaded programs
which use libc can share the same one, so they need less real memory to
load.

Additionally, you can replace the on-disk version of a library, and all the
dynamicly linked programs can take advantage. Say if you fix bugs, or add
new features which need no new program changes.

The draw backs are that they are a little slower as the process which permits
the sharing often adds extra steps to subroutine calls. Not many, but
a few.

Also, if anything happens to the dynamic libraries, all the programs using
them DIE. If you delete the files in /usr/lib, EVIL things happen to your
system. Because of this, the tools in /bin and /sbin are linked to use
static libraries. That way each program is totally self-contained. It could
be the only program on the disk, and it would work.

If shared libraries work right for the package you're using, I'd
vote for using them.

Take care,

Bill