All about static libraries in C

Let’s talk compilation first!
If you don’t know anything about compilation, I advice you to go and have a look on the previous article and here is the link.
In the last stage of compilation (the linking), the linker will arrange the pieces of object code so that functions in some c files can successfully call functions in other ones. It will also add pieces containing the instructions for library functions used by the program.
So what is a library? and why use one?
In C, libraries are archives of object code files that function as a single entity. They provide a list of indexed symbols and definitions that are linked to an executable at the end of compilation. The standard library files can be found in /usr/lib/
and /lib/
on a Linux system.
why use one? Have you ever written the same code over and over again in different programs and you wish there was some magical way to just skip that part? well that’s what libraries do! they save us time and energy!
There are two types of libraries and here’s the difference:

when using static libraries, the linker adds the object files mentioned in the program from the library to itself, which increases the size of the program each time. But in the case of dynamic libraries (or shared libraries), a program called a dynamic loader checks out which shared libraries were linked with the program, loads them to memory, and attaches them to the copy of the program in memory.
then why use static libraries and not dynamic libraries?
basically, run time! add to that, a static library is untouchable because it lives inside the executable file.
Let’s see how to create a static library!
the tool used for creating a library is ar , it stands for archiver.
ar can be used to create, list, modify libraries.
But let’s start from scratch:
1- Create all your files in one directory:

2- Create a header file with your function prototypes and include the header file in your C source files

3- Compile all your .c files without linking:
we do that by using gcc to compile them but with option -c which returns the same file names but as object files and with extension.

4- Create the library <libholberton.a>
Now we use the command we talked about earlier, ar. The syntax is very simple:

This command creates a static library named ‘libholberton.a’ and puts copies of all the object files in it. If the library file already exists, it has the object files added to it, or replaced, if they are newer than those inside the library. 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. Libraries have the .a
extension and lib
as a prefix before the library name.
Care to check? use the ar -t command!
5- Index the library:
why? well, just like when looking for a specific chapter in a book you look at the table of contents to find it faster. Compiler does the same thing if our archive is indexed. And to index our library (archive) we run this command:

On some systems, the archiver already takes care of the index, so ranlib is not needed . However, because 'ar'
and 'ranlib'
are used by many makefiles for many packages, such platforms tend to supply a ranlib command that does nothing.

Now that we’ve created a library, let’s have a look on how to use it!
After we created our archive, we want to use it in a program. This is done by adding the library’s name to the list of object file names given to the linker, using a special flag, normally '-l'
. here’s how:
gcc main.c -L. -lname_of_your_library_here -o your_exe_file_name
This will create a program using object file main.o created from main.c and any functions we used from the specified static library.

And by that, we pretty much covered everything you need to know about static libraries. Stay updated for the next article we’ll be talking about Dynamic libraries.
Enjoy Linux! :) Enjoy C x)