Differences Between Static and Dynamic Libraries

Hachem Hfaiedh
4 min readMay 4, 2020

--

What’s a library ?

A library in C is a collection of header files, for example _putchar() or _atoi().Libraries are in-built functions which are placed in a common place. Libraries come in two flavors: static and dynamic

Why use libraries in general :

We can make use of these library functions to get the pre-defined output instead of writing our own code to get those outputs. It is easier to create a static or dynamic library with the functions that you need. Libraries also save time in that they make functions reusable in multiple programs.

How do libraries work ?

A library is exactly like an executable, except instead of running directly, the library functions are invoked with parameters from your executable. a library consists of one or more object files, which consist of object code that is usually the output of a compiler. These object files are then turned into a library in the form of an archive

Dynamic Library Creation (Linux only) :

1. gcc -fPIC -c *.c

The “-c” option generates the object files like the example below. The “-fPIC” flag generates position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table.

2. gcc -shared -o libname.so *.o

then we pass the output.o files through gcc again with “-shared” as linking option, this is all we need.

The command “nm” lists symbols contained in the object file or shared library.

Use the command nm -D libname.so

export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
Because a program needs to know where to look for library files, we must add that location to the environmental variable LD_LIBRARY_PATH.

Static Library Creation (Linux only)

1. gcc -c *.c

Using the gcc command with the “-c” flag to generate the object files needed for the library like the example below.

2. ar rc liball.a *.o

then use the “ar” command, it takes the “r” flag telling the compiler to update existing object files, and the “c” flag to create the objects in the file that doesn’t exist yet.

After you create or modify the library, you need to index it. which is done with the command ranlib.

If we want to see the contents of our library, we can use the ar option -t.

ar -t libname.a

The command “nm” lists symbols contained in the archive library:

Object symbols in static archive libraries are categorized using the source and object file hierarchy of the library:

How to use them

$ gcc main.c -L. -lholberton -o main

In order to use the libraries you will need to tell the compiler with the ‘-L’ flag to find the libraries needed in the current directory. The ‘-l’ flag tells it we want to include library files. And ‘holberton’ tells it to look for the library libholberton.so. It’s important to leave the ‘lib’ and ‘.so’ out of the flag because the compiler already identifies library files that way.

Differences between static and dynamic libraries :

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries on the other hand, exist as separate files outside of the executable file.

Pros and Cons of Both Libraries:

Dynamic:

  • Pros:

— Multiple running applications can use the same library without the need for each to have their’s own copy

— Can be modified without a need to re-compile

— They are smaller cause there is only one copy that is kept in memory

  • Cons:

— Programs are dependent on having a compatible library. If a dynamic library for example becomes corrupt, the executable file may no longer work

Static:

  • Pros:

— It never has compatibility issues, since all the code is ino an executable module. It’s untouchable because it lives inside the executable file

  • Cons:

— Takes longer to execute because it loads into the memory each time it is executed

— It’s code is locked into the final executable file and cannot be modified without a re-compile

— They are bigger in size since external programs are built in the executable file

--

--