Static vs Dynamic libraries

Francisco Guzmán Herrera
4 min readSep 8, 2020

Libraries in Programming are files that generally contain functionalities that programs can use to perform a specific task such as adding two numbers. In this post, we will see the difference between the two libraries the static and dynamic libraries.

Why using libraries in general

Let’s imagine that we have many files, each with specific functionality that we know will be used by a program. One way to organize all these functionalities is to make a library with all these files, and we can even make libraries with related functionalities so that if we find an error we can solve it faster.

Another reason to use libraries is to store functions that we use frequently in a programming language. In order not to have to move each function from one place to another whenever we need it, we can add them to a library and thus have them always available.

How do they work

We already know that libraries contain functionalities now let’s see how each one of them works:

Static libraries

The static libraries when the code of the program is compiled would be added with the program to generate the program completely.

The problem with this is that the final program will have the weight of the code plus the weight of the library, so if we have a lot of functionalities in our library, the program will weigh a lot.

Another problem is that if we update a functionality we must compile our program again so that it can be updated.

Dynamic libraries

The dynamic or shared library, unlike the static library, does not mix with the program code to generate the complete program.

This has the advantage:
1) That the program is not heavier as it is separated from the final program

2) Is easier to update since being separated if we update the dynamic library automatically the program will be updated

3) And we can share the functionalities of a library with other programs at the same time

How to create them in Linux

let’s do this example with the programming language c

To create our library we must first convert our files that contain the functionalities that we want to be in the library

We will convert them to object files, that is, we compile them but we do not join them to create the final program

The command used to convert files in case we want to create a static library is:

$ gcc -c file.c file2.c

And the command used to Dynamic libraries is:

$ gcc -c -fPIC file.c file2.c

In each case we use the gcc -c command where gcc is the compiler we use and the -c option is to tell the compiler not to link the files since, as we saw before, we do not want to make the final program.

Then follow the files that we want to compile separated by spaces.

Unlike the command used to create the files in the case of the static library in the dynamic library another obcion -fPIC is used.

The-fPIC option is used so that the code in the library is independent, that is, so that when any program requires it, it works correctly.

Our files will now have a .o extension and we can create our library it will be called holberton

To create a static library we use the command:

rar rc libholberton.a file.o file2.o

To create Dynamic library:

gcc -shared -o myholberton.so file.o file2.o

How to use them in Linux

Now that we create our library we are going to compile it with our program to be able to use it

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

where gcc is the compiler command main.c is my program. -L is to specify the directory that in this case is the current one we indicate it with . and -l is to indicate the name of the library that is holberton(we must not put lib at the beginning nor the extension .so since the command recognizes it as a library file). and with the obcion -owe put the name which in this case is len

Now the static library is ready to use for the program len but the dynamic one needs one more step

We must add the dynamic library dependency

To see the current dependency we use the ldd command and the name of our program

$ ldd len

We see that in the second line it says that our library was not found.

To add our library we use the following command:

$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

This command will add our library to the system dynamic library list, so if we run the ldd lencommand again

$ ldd len

We see that now if we find our library and it is ready to be used by our lenprogram and other programs that require it.

What are the differences between static and dynamic libraries? and What are the advantages and drawbacks of each of them?

  1. The static library is added to the final program while the dynamic is a different file
  2. Static library can be used after final program is created while dynamic must be added to system list of dynamic libraries
  3. Dynamic library can be used by many programs, while static can only be used by one
  4. If there is an update on a library, the static must be compiled again to update, while with the dynamic it will only replace the file

--

--