Know the differences between static and dynamic libraries

Andres Aristizabal
5 min readDec 15, 2020

There is a popular saying that says: “there is no need to reinvent the wheel” and this is a great truth for life and especially for programming. Currently we have at our disposal countless code routines that have already been written and tested over and over again, routines that make our life easier and allow us to continue building on what has been built. Can you imagine having to implement your own version of printf for C or print for Python every time you wanted to print something on screen?

The miracle of libraries… why are they good?

Libraries are the means by which we can easily save and categorize the code that we use daily in our applications over and over again, without the need to copy and paste them from that forgotten source code that for some reason is not in our remote repo. Libraries in essence allow us to compile code routines that we can reuse whenever we want, gaining portability (you can share them) and greater control over their update and support.

How do they work?

Basically you can find two types of libraries: static libraries and dynamic libraries. Each of them has its strengths and weaknesses, let me tell you what they are:

Static libraries

A static library is one that is composed of object codes ready to be joined with our file in the linking process. We find several interesting points in this:

  1. The source codes of the different code routines that the library contains are already compiled into binaries.
  2. These binaries will only be mixed with our files in the linking phase of compilation.
  3. Thanks to dealing with object files the compilation process becomes much more efficient (speed)

In conclusion, static libraries are nothing more than packaged object codes ready to be attached to our binary when we compile our program, this feature is in turn its greatest advantage and its weak point.

From a practical point of view, having your program ready to run without dependencies on “third parties” helps to have great portability, it also makes your program to a certain extent more secure and is a good option for major projects, but … why are they not good for large-scale developments?

As the library is built into the executable it becomes very difficult and tedious to update it on a large scale, think about this, if you add a new feature or fix a bug, you would have to recompile every program that uses that library for the changes to come in. It is a task that can easily get out of hand.

Dynamic libraries

Dynamic libraries, as their name indicates, are a compilation of object files that are loaded into memory at runtime, making all the code routines that the library has at the disposal of our program. This approach brings great advantages, here are some of them:

  1. They allow us to keep our programs light since the library binary is not directly integrated into our program, as opposed to static libraries.
  2. They allow great scalability since updating a library only implies that, updating the file and distributing it again, it is not necessary to compile the programs that consume them again, which makes it very easy to maintain more robust systems.

It could be said that dynamic libraries are the perfect option, however, you have to be careful with them since when they are integrated into our programs at runtime they could leave doors open in terms of security.

How to create them

In a Linux environment it is very easy to create these libraries with the extension .a for the static ones and .so for the dynamic ones, here I leave you the necessary commands for each one of them.

Static libraries

First compile to object files all the routines you want to include in the library and then run this command on your files

ar crs libLibraryName.a *.o

We used ar to create the library called libLibraryName.a (notice the prefix lib for library and the extension .a for archive), containing all the object files we had in our working directory. With the ‘c’ flag we can create the library if it doesn’t already exist. On the other hand, the ‘r’ flag is used to replace older object files in the library, with the new ones. The last flag ‘s’ basically indexes the library.

Dynamic libraries

Just compile with gcc each source file *.c, up to the point right before the linking occurs (using the '-c' flag) to obtain the object files, and make sure the code is position-independent (using the '-fPIC' flag). Altogether it looks like the following command (you can add some more flags like -Werror -Wall -pedantic & -Wextra as well):

gcc *.c -c -fPIC

Finish the compilation of those object files into a dynamic library (using the '-shared' flag). With the '-o' flag you can determine the name of the library, just remember to name it with lib at the beginning and end with the file extension .so

gcc *.o -shared -o libLibraryName.so

Lastly, the program should be able to locate such libraries when executed; therefor you can export the environment variable $LD_LIBRARY_PATH (a colon-separated list of directories to search), which can be done with the following command:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

How to use them

Once the library is ready, you can add it to the list of files given to the linker using the flag -l. We can see the implementation in the next example:

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

Here we created an executable program called main, using the source file main.c and any information required from the libLibraryName library (notice the omission of the prefix and the extension when referencing the library on the command line, it is only posible because of te -l flag). The linker attaches these parts back to the name of the library to create a name of a file to look for. The ‘-L’ flag tells the linker that libraries can be found in the given directory (in this case ‘.’, that refers to the current directory), in addition to the standard location where the compiler looks for system libraries.

I hope this article has helped you to become more familiar with the concept of libraries, if you are interested in learning more about them, here is an article I wrote about Static Libraries. if you liked this topic I encourage you to share this article and expand the knowledge. See you soon…

--

--