Dynamic and Static Linking

Justin Wells jread at semiotek.com
Mon Apr 3 01:01:05 UTC 2000


On Sun, Apr 02, 2000 at 10:11:15PM +0100, W . Yip wrote:

> 1) What is dynamic linking? Is it the 'ln' command in Linux for symbolic
> linking?
  
> 2) What is static linking? Is that a link to libraries on your hard disk?

Ian's answer was a good one, but I want to add some more points, and explain
it in a lower level of detail so you can think about the implications. I 
would like more people to be thinking about this :-)

First yet another review, but at a bit lower level. When you write a program
that looks like this:

     #include "sqrt.h"
     x = sqrt(y);

and then you compile it, you are using a sqrt() function defined by some 
other library somewhere and asking that it be included into your program. 
Whether you have "dynamic" or "static" linking depends on exactly when 
this sqrt() function gets combined with you program (and how).

The simplest form is "static linking" which means that when you build your
program, the compiler copies the binary text of the sqrt() function right
into your program. If you distribute your program, you will therefore be
distributing the text of this sqrt() function. Thus with static linking,
you clearly have created a derivative work of the sqrt() function.

The more modern way is "dynamic linking" which means that when you build
your program, the compiler DOES NOT copy the text of sqrt() into it, it 
simply records a note that your program wants to use a sqrt() function and
will require one at runtime. Now when you distribute your program you are
not distributing the text of the sqrt() function--only a "reference" to it (a 
description of it so that it can be properly located later, and a list of 
the places in your program where it should be linked in).

With dynamic linking, when your program is finally run by an end user 
the operating system (in particular the "link loader") will examine your
program and notice that it expects to be provided with a sqrt() function.
The operating system will find one, and "fix up" your program so that 
the notation saying a sqrt is wanted is actually transformed into a 
direct pointer to the text of the sqrt() program (usually by replacing
the reference with a valid pointer to the function). In order to save 
memory, if two different programs dynamically link to the sqrt() function,
only one copy of sqrt() will be loaded into memory and both programs will
share that copy of it.

This is how things are in the C programming world. The reason why I wanted
to add more to this message is to point out that it gets worse than this,
from a legal confusion point of view, because of languages like Java, and
technologies like CORBA. 

The big advantage of dynamic linking is that if there is a bug in the 
sqrt() function and someone fixes it, your program automatically starts
using the fixed version, because it isn't combined until runtime. With 
static linking the old buggy version was actually copied into your 
program's binary image.

In Java and CORBA not only do you not copy the sqrt() function into your
program at "compile time", you don't even necessarily do it at load time:
the operating system (or Java interpreter) can load and unload different 
versions of the sqrt() function during execution. 

In the dynamic link case you don't actually know WHOSE sqrt() function 
you are going to use either. At runtime, you might use the one that you
have on your computer--or you might use a totally different one one 
someone elses's computer that happens to be fully compatible with the
one you compiled against (it has the same "function signature", meaning
that it declares the same name, same return type, and same arguments).

Worse still, the function you wind up using may not even be on the
same computer as where the program is executing--it may be provided
over some network-function-call protocol like Java's RMI or CORBA
or DCOM. The program using it has effectively no information about
the sqrt() function, except that one exists and will supposedly
behave as expected.

So the problem for the GPL and other copyright licenses is whether or 
not a dynamically linked program is a derivative work of the sqrt() 
function, who made the derivative (the program author, or the person 
who caused it to link), and what the implications are if there are 
different choices for the sqrt() function (different vendors, etc.)

> 3) Can you give me some examples of dynamic linking (un)covered by the GPL?
> Is BeOS a case in example?

As Ian said, DLL's on windows, and .so files on Unix are dynamically linked
libraries. Also, *all* Java programs are dynamically linked.

Justin




More information about the License-discuss mailing list