<Query> Regarding static and shared (dynamic) libraries linking

Javier Martinez Canillas martinez.javier at gmail.com
Wed Mar 23 07:48:01 EDT 2011


On Wed, Mar 23, 2011 at 10:25 AM, Leelakrishna A
<amudala.krishna at gmail.com> wrote:
>
> Hi All,
> I have few doubts regarding static and shared (dynamic) libraries linking.
> Can anybody please clarify it.

Hello Leelakrishna,

This list is about Linux kernel development, not user-space
application development. There is a special mailing list for that
purpose that is: linux-c-programming at vger.kernel.org.

But I will try to answer your questions:

> There are two kinds of libraries - static libary(.a file) and shared library
> (.so file).
> Also there are two types of library linkages static linkage and dynamic
> linkage.

Yes, static linkage is used with static libraries and dynamic linkage
is used with shared (dynamic) libraries. You can't static link a
shared library nor dynamic link a static library.

Although there are tools that create a pseudo-static binary, but is
more a hack than a normal behaviour (i have never used those tools so
I can't tell you how well they work).

> So Static library can be linked to an executable in either ways (Statically
> and dynamically).
> in the same way shared libraries can be linked to executable in both the
> ways (Statically and dynamically).

As I said you can only static link a static library. For example the command:

gcc -static main.c -lhello

Will create an executable statically linked with the static version of
the library libhello (libhello.a) and not the shared (dynamic) version
of the library (libhello.so).

> Here my doubt is what is the use if we statically linked the shared library
> to the executable binary
> (by this way the shared library is part of the executable) and what is the
> point in calling it as shared libary if it can be linked statically to the
> executable file.
>

The shared library never is part of the executable. Linkage can happen
in three different moments: at compile time (statically), at load time
(if you dynamical linked your program to a shared library) or at run
time (dynamic loading) by requesting the dynamic linker to load and
link shared libraries.

Only when you static link a static library with a program, the library
becomes part of the executable.

If you use dynamical linking your binary has information that tells
the loader that it has to load the shared library and resolve symbols
upon execution while dynamic loading allows you to load a shared
library and call a particular function even when you didn't dynamic
link your program against the library.

There is a small but powerful API to do dyamic loading (dlopen, dlsym,
dlerror, dlclose). It is used mostly for programs that implements
plug-ins and to do function interposition (also known as function
hijacking).

> If we linked the shared library to the executable file using dynamic
> linkage, executable file will pick the library at run time.

It is not the executable who pick the library but the loader.

> Here my doubt is to pick up the library at run time who will load the
> library into the memory and how will the executable know about the address
> of the function in the library to invoke the function.
> Thanks in advance,
> Leela krishna.

It is the loader who pick the library upon execution and bind the
binary symbols with the ones found in the shared library. Which
library it will pick depends on your configuration (i.e: the value in
your LD_LIBRARY_PATH var and ld.so.conf file).

The picked library doesn't even has to be the same that was used to
dynamic link at compile time. To see what libraries the loader will
pick for a binary execute the ldd command.

$ ldd binary

You can also force to load a specific shared library before any other
(including libc) by setting LD_PRELOAD to the library path.

Hope its helps.

Best regards,

-----------------------------------------
Javier Martínez Canillas
(+34) 682 39 81 69
PhD Student in High Performance Computing
Computer Architecture and Operating System Department (CAOS)
Universitat Autònoma de Barcelona
Barcelona, Spain



More information about the Kernelnewbies mailing list