What are the differences between static and dynamic libraries ?

Natalia Ponsard
3 min readDec 14, 2020

--

A library is a collection of subroutines that allows the code of those subroutines to be used as separate modules.

How do they work ?

Image 1. How do libraries work.

How to create static library in Linux ?

Image 2. How to create a static library in Linux.

How to create dynamic library in Linux ?

Image 3. How to create a dynamic library in Linux.

How to use them in Linux:

Static libraries:

To compile our main file, main.c, we need to tell the compiler to use libraries. So that the compiler knows where to look for libraries, it needs to be told the directory in which they are contained and the list of these libraries. The library directory is specified with the -L switch.

In our case the library is in the current directory, so the path to it will be in the form of a dot (-L.). The libraries used are listed with the -l switch, followed by the name of the library without the lib prefix and the ending .a. In our case, this key will look like -lfunctions. Now all with one command:

Dynamic libraries:

You can compile your code like this:

The expression, -l combined with holberton tells the compiler to look for a dynamic library called libholberton.so. The -L flag tells the compiler to look in the current directory for the library file. Typing and executing will generate an executable file called test.

What are the differences, advantages and drawbacks between static and dynamic libraries ?

In the table below, I have collected the features of each type of library.

Image 4. The differences, advantages and drawbacks between static and dynamic libraries in Linux.

--

--