The C Static Libraries

Pierre Forcioli
3 min readOct 9, 2020


Today, I’m going to tell you about C Static Libraries.
- Why should you use them?
- How to create them?
- How do they work?
- How to use them?

You can’t wait to know everything, so let’s begin!

Why do I need to use C Static Libariaires?

The most significant advantage while using Static Libraries instead of Dynamic ones is that the application can be certain that all its libraries are present and that they are the correct version. This avoids dependency problems. Static linking can also allow the application to be contained in a single executable file, simplifying distribution and installation. Also, the size of the executable is greater than in dynamic linking, as the library code is stored within the executable rather than in separate files.

How to create them and how they work?

First of all, you need to have all your .c files you want to add. It’s easier to store them in one directory, like this, it will be easier to link them together for the next steps.

Once all the functions you want to add in your library are all set, you have to make them object files. If you don’t know what I am talking about, just read my fabulous blog about the compilation process in C.

$ gcc -c *.c

The basic tool used to create static libraries is a program called ar, for archiver. This program can be used to create static libraries (which are actually archive files), modify object files in the static library, list the names of object files in the library, etc… To create a static library, we can use a command like this one:

$ ar rc libmylib.a *.o

This command creates a static library named libmylib.a and puts copies of the object files (written here as *.o) in it.

— 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 (if they are new ones)

And that’s it, you can check what’s inside your library by executing this command $ ar -t libmylib.a

[pierre@ubuntu 0x09-static_libraries]$ ar -t libmylib.a

This is an example of our first C library made at Holberton School.

Also, a list of symbols (metadata about the addresses of the variables of the program and its functions) can be displayed with $ nm libmylib.a

You will see later than when we will compile our main fils with this library in argument, the symbols of the functions will be added to our executable.

[pierre@ubuntu 0x09-static_libraries]$ nm libmylib.a 

0000000000000000 T _isupper

0000000000000000 T _memset


0000000000000000 T _strcpy

0000000000000000 T _putchar
U write

After an archive (in this case, our library) is created, or modified, there is a need to index it. This index is later used by the compiler to speed up 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 index a library by using the following command:

$ ranlib libmylib.a

How to use them?

Once your static library is created by following the upper steps, you can use it by invoking it as part of the compilation and linking process when creating a program executable. Here is how:

$ gcc main.c -L. -lmylib
  • -l<library name>, without the lib prefix and the .aextension
  • -L : path to the library

Your main.c file may contains functions added to your library by only including the header fils wich contains the prototypes of the functions, it will compile without needing the add of all the functions files.

That’s it!