Thibaud explains… Static libraries in C

Thibaud Poncin
3 min readOct 9, 2020
Wait… is that the right kind of library?

What are static libraries and how do they work?

Libraries are files containing multiple object files, that can be used as a single entity in the linking phase of the compiling process. In other words, it’s a collection of binaries of resources that can be used by any program that calls it.

Static vs. Dynamic libraries

Dynamic libraries are stored as separate entities (and thus, files), to your program. While this means you only need one copy of your library for every one of your executables, that also creats its own issues: 1/ the access times are slow (because you need to reach another file and run that bit of code from there), and most importantly 2/ because you create a situation called dependency, where your program cannot function if the library files are not present, or worse, not of the version that your program expects.

On the other hand, the Static libraries are, as their name implies, static. When the compiler links a static library, the complete library file is prepended to your program. While storing the library inside your program makes for lightning fast access times and solves the dependency issue, this also means that the executable file you end up with will be bigger (the whole of Glibc adds around 2 Mb to any executable that uses it).

Creating static libraries

To create a static library, you first need code! Let’s assume you’ve been smart and already put them in the same folder. We’ll need to convert this code into machine code, stored as .o object files, before we can proceed with creating the library. Let’s do just that:

$ gcc -c *.c

Now we’re talking. Since we now have all of our files compiled into binaries, let’s tell the computer thingy that we want to mesh them together to form a library! To do this, we’ll need another command: ar (which stands for archiver). This program is not only used to create static libraries, but also update them, display the files stored inside of them, etc.

Let’s create our static library, with all of our .o files!

$ ar rc libmysuperlib.a *.o

Et voilà! We’ve just created our first static lib, libmysuperlib.a, that contains all of the object files we’ve created during the previous step. The r argument tells ar to replace older content in the library with the provided one if it’s newer, and c orders the creation of the library if it doesn’t already exist. Oh, and by the way, library names always start by lib!

Once your library is created, the only thing left to do is index its content, which will help the compiler make sense of the symbolsThis can be done with the ranlib command, as follows:

$ ranlib libmysuperlib.a

Using static libraries

Invoking your shiny new static library is as easy as adding two flags to your compilation command:

$ gcc main.c -lmysuperlib -L.

Here, -l<name of your lib> indicates the name of your library (without both the .a extension or the lib prefix) and -L<path> indicates the path to your library file.

All of the code that you included in your library archive has now been prepended to your main.c code!

--

--