Photo by j zamora on Unsplash

C static libraries

Why use libraries

--

When you write a program in C, you have the opportunity to use functions that were written by other people. Like in a library looking for useful books, your program can use several libraries and find useful function in them.

These functions are in machine code and stored in “object files”, with extension *.o. If you recall my post “What happens when you type gcc main.c in your terminal”, I explained that the c program was converted to assembly code and then object code, before the linker.

Thus, a static library is a file, containing several object files. They are linked to your program during the linking phase of compilation.

How to create a static library

First, to create a static library, you need to have your functions written in object code. Let’s create a folder where we have a couple of functions written in the following files:

anso$ ls -1
0-print_z.c
1-print_alphabet.c
2-print_tebahpla.c
3-print_base16.c
4-positive_or_not.c
5-print_number.c

To do so, you can run the following command that will tell gcc to stop before the linking phase.

anso$ gcc -Wall -pedantic -Werror -Wextra -c *.c
anso$ ls -1
0-print_z.c
0-print_z.o
1-print_alphabet.c
1-print_alphabet.o
2-print_tebahpla.c
2-print_tebahpla.o
3-print_base16.c
3-print_base16.o
4-positive_or_not.c
4-positive_or_not.o
5-print_number.c
5-print_number.o

Then, we use the archiver tool to create the static library which is actually an archive. ar creates, modifies, and extracts from archives. With the following command, we create a static library using copies of all the object files from our current directory. Libraries in GCC start with lib and have the extension .a on Unix-like systems (and “.lib” on MS Windows):

anso$ ar -rc libperso.a *.o

The 'c' flag tells ar to create the library if it doesn't already exist. The 'r' flag tells it to replace older object files in the library, with the new object files.

For the library to be operational, we need to index it. This speeds up the symbol-lookup inside the library, and to make sure that the order of the symbols in the library won’t matter during compilation.

We can do this by simply writing the following command:

anso$ ranlib libperso.a
  • How they work

How to use static libraries?

We can only link the libraries with our program if we tell the compiler to. This is why we need to add the '-L' flag and pass the name of the library to the compiler.

anso$ gcc main.c -L. -perso

Note that we don’t write the "lib" prefix and the ".a" suffix when mentioning the library on the link command. The linker will attache these parts back to the name of the library to create a name of a file to look for. If you want you can still add an “l” as a prefix:

anso$ gcc main.c -L. -lperso

In addition, we add a dot '.' to the L flag to mention that the library can be found in the current directory in addition to the standard locations where the compiler looks for system libraries.

How do static libraries actually work

When you compile your program, the source code of function one(), like in the example below, is included in the final executable file.

https://www.youtube.com/watch?v=eW5he5uFBNM

This behavior is actually a drawback of static libraries because it increases the size of your executable file.

Another drawback is if the library is updated, your executable file will not get the new updates, unless you compile it again with the new library.

You link the newly created library in your program by writing the following line at the beginning of your program:

#include <libperso>

--

--