Static libraries in C language

Steffany Naranjo Vargas
4 min readJul 5, 2020

What is a C library?

Static libraries are just collections of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime. This last comment seems obvious, as we already know that object files are also used only during the linking phase, and are not required during runtime — only the program’s executable file is needed in order to run the program.

One of the tools that compilers supply us with are libraries. A library is a file containing several object files, that can be used as a single entity in a linking phase of a program. Normally the library is indexed, so it is easy to find symbols (functions, variables and so on) in them. For this reason, linking a program whose object files are ordered in libraries is faster than linking a program whose object files are separate on the disk. Also, when using a library, we have fewer files to look for and open, which even further speeds up linking.

Why use static libraries?

The static libraries made our lives easy. We know that the software is made up of functions, and the functions are like mini programs that can be “called” by another program to perform some type of action such as a computation or modification of input. With the static libraries we can store many object files that can be used in a linking phase of a program.

When the functions get more and more complex, they can become very long and make the main program hard to read. Static libraries help keep programs clean and concise for humans.

How they works?

As we may know, when you compile a source file you get an object file. Its extension may be .o. A static library is basically a collection of object files, kind of like a .zip file but probably not compressed. The linker, when trying to generate an executable tries to resolve the referenced symbols, locate in which object file (be it in a library or otherwise) they are defined and links them together. So, a static library may also contain an index of defined symbols in order to facilitate this.

How to create a static library step by step?

  1. First you have to create a directory with all the relevant files including the header file remember that the header file contains the macros #ifndef <HEADERFILE>_H and #define <HEADERFILE>_H at the top and #endif at the end so that the header file is only defined once instead of each time it is called.

2. Add all the .c files to the directory and compiled using this

You will have a .o copy of all of your .c files. Something like this

3. In order to create a library you will use this command.

Now the library is created with the name that you choose.

4. After an archive 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 (this will be better understood when we take a deeper look at the link process at the end of this tutorial). The command used to create or update the index is called 'ranlib', and is invoked in this form

To remember: On some systems, the archiver (which is not always ar) already takes care of the index, so ranlib is not needed (for example, when Sun's C compiler creates an archive, it is already indexed). However, because 'ar' and 'ranlib' are used by many makefiles for many packages, such platforms tend to supply a ranlib command that does nothing. This helps using the same makefile on both types of platforms.

How to use a static library

To invoke the library we have to use the compiler gcc in this way

Now you just have to run the file and you will have a executable file with all the functions in the library.

I hope this blog helped you to understand better what is a static library and how to use it!!

--

--