Static libraries in C: creation and use
Diana Parra
Backend developer at Capital Lab by Credicorp Capital | Degree in natural sciences
A simple analogy with a common library, like the ones we all know, where a lot of books of different topics or genres are stored, can be the perfect analogy to talk about static libraries in C.
A static C library is a file that contains a collection of object files. To better understand, we must remember the four phases of the compilation process in C: pre-processing, compilation, assembly and linking. Specifically, we will focus on the last phase, the linking phase, where the object file (.o) becomes executable from the link that is created between this file and the libraries it uses.
Why use libraries
This question is fundamental because we already know what happens in the process of compiling a program, but why do I have to use libraries? are they strictly necessary?
Well, libraries are one of the primary tools of every programmer because a library file contains a collection of functions and statements to be used in other programs and even by other programmers (like books in libraries, right?). We may think that libraries make our life easier, and in fact they do!
Static libraries are linked into the program during the linking phase of compilation. They are not relevant for runtime.
How they work
Now we will see how to create a standard library, but we can start by saying that static libraries are usually indexed, which means that all the symbols of the function are visible and it will help the linker to go faster, because it will perform this action having only one file with enough information to compile the program: less compilation time and more efficiency.
How to create them
The first thing you must have is a header file with each of the prototypes of the functions that you want your library to have. Don't forget to place the macros at the top and bottom of your header file (.h extension). It should look something like this:
Now that you have your header file ready, you must separate each of the source files (.c) that correspond to those functions you placed in that file (.h). You can gather all the files in a single directory and compile them all into object files (.o) with the following command:
$ gcc -c *.c
Note: The -c flag means that we will compile and assemble but not link.
Now you are ready with the necessary files to create the static library (.a) and it is so simple that with a single command all the .o files will be packed in an .a file (see ar man):
$ ar -rc libholberton.a *.o
Note: The -r flag is to ensure the existence of a file in the library to be replaced and the -c flag is to ensure that if a file does not exist, it is created.
Your library is now created but there is one last thing to keep in mind. When you start adding new items to your library (and even now that you have just created it) you must index the library, this is done with the ranlib command (see ranlib man):
$ ranlib libholberton.a
How to use them
Here are the latest commands you need to know to use your library.
- If you want to see the contents of your library:
- You can view the symbols in your library using the nm command (see man nm):
- Finally, to use the static library as part of the compilation and linking process when creating a program executable:
$ gcc main.c -L. -lholberton -o main
The -L flag specifies the path to the given libraries and the accompanying "." is referring to the current directory. The -l flag, as you can notice, simplifies the library name by dropping the "lib" prefix and the ".a" suffix.
And that's it, you can run your "main" program, like this:
$ ./main
You can find more information in the following links:
- https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html Building And Using Static And Shared "C" Libraries
- https://www.cs.swarthmore.edu/~newhall/unixhelp/howto_C_libraries.html C Libraries