C — Static libraries

Diana Henao
4 min readMar 2, 2021
Photo by Cookie the Pom on Unsplash

First things first, what is a library in C?

When you begin to program more and more, maybe you notice that some parts of the code are reused. If we have functions that made that thing that we need to, why we need to copy and paste that again?

Here is where the libraries take place. A library is a file (.a) that contains multiple object files (.o). The libraries allow us to have some handy functions separate and compile.

These useful libraries allow us some advantages:

  1. We don’t need to re-write the code every time we need to use it.
  2. Because the code compiles, the code should be reliable. And can be used in any program.

To create a library, we need two things: The header file (.h) with the function’s prototypes and the source file (.c) with the function’s code.

Static and Dynamic libraries:

There are two types of libraries. The choice of one or another depends on if the program is large or small. Usually, for small programs, a Static library is a good option.

In this opportunity, I only write about Static libraries.

When you use Static Libraries, the program copies all the functions it needs to the executable file when compiling. And that’s why using Static libraries is preferred in medium or small files. Regardless of the large size that the executable can become, with the addition of the functions, this result has two advantages:

1. The compiled program can be used on another computer without the need to copy the libraries themselves.

2. It is faster in execution since it does not need to search the file every time you want to find a function.

Maybe you can remember my post about What happens when you type gcc main. c? Well, integration of the libraries to the executable file, when is compiled, occurs in the linking step of compilation.

Steps to create a Static Library and where the library comes in our compiling process.

How to create a static library?

There are only two or three main steps to create a library in C:

  1. Compile and obtain object file (.o): We need to compile our source files. It is necessary not to obtain the executable files, but the object file. This can be done using the gcc compilation -c option.
-c option -> compilation before linking. We can obtain the .o file

2. Create the library (.a): Using the ar command (ar from archiver) object files are inserted into the library (.a). The r flag replaces the old object files in the library (if there are other versions of the object files). The c flag is responsible for creating the library if it did not exist before. The s flag adds an index to the file.

rcs options to create a library if it doesn’t exist, replace old object files in the library, and add an index

3. Index the library: This step is not always necessary. If you used the s option in the ar command, this step is NOT necessary. The index is used to increase the search speed within the library by the compiler. It can be done with the command ranlib.

ranlib is not always necessary

Aaaaand that's it, now we have a library called “lib_library_name.a” ready to use!

How to use it?

Ok, you only need to change some things in the usual gcc compilation command. You can see in the next image, that -L and -l are the new and unknown options.

Command used to compile a C file and creates an executable file using created static library

Here we said to the gcc compiler that we need to search the library in the current directory (with -L. option). The option -l is used to name the library itself. Notice that the “lib” and the “.a” are removed from the original library name “lib_library_name.a”. With this option, the library “_library_name” passed directly to the linker of gcc compiler.

Now, we should have the executable file readyyy to use! :)

References:

Abellán, Javier. 2007. Librerías estáticas y dinámicas: http://www.chuidiang.org/clinux/herramientas/librerias.php

Keren, Guy. 1998. Building And Using Static And Shared “C” Libraries: https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html

Saha, Abhijit. 2019. Static and Dynamic Libraries | Set 1: https://www.geeksforgeeks.org/static-vs-dynamic-libraries/

--

--

Diana Henao

Programmer in training, because there is always a lot to learn. I’m not the best, but I’m trying my best!