Static Libraries in C

Melisa Rojas
4 min readMar 8, 2021

All you need to know

A library is a collection of code routines (functions, classes, variables, and so on) that can be called upon when building our program, so instead of writing it ourselves, we can go and get it from something that has already been written and optimized. That is where the idea behind libraries comes from. We are reusing blocks of codes that have come from somewhere else.

Therefore, static libraries in C are collections of object files that are linked together when a file gets compiled into an executable file. They are a set of functions that we use frequently and that are generally grouped according to their usefulness.

Why use libraries in C?

The main reason for using a library is to allow code reuse, which saves considerable development time.

How does it work?

A static library is a file containing a collection of object files that are linked into the program during the linking phase of compilation.

To better understand how it works, let’s see the steps of the compilation process and the files that are generated, in the following graphic.

Compilation Process

We can see that when a program is compiled, the compiler generates an object file (.o) from a source file (assembler phase). After generating the object file (.o), the compiler also invokes the linker. Static libraries (.a) are added during the linking phase of the build process. During the linker phase: linker links access all libraries to link functions to the program.
Finally static libraries are just a collection of object files that the linker merges with another object file to form a final executable.

How to create static libraries?

In order to create a static library from scratch, we must follow the following steps:

  1. Prepare source files
  • Create all your source files (.c) in one directory. The source files contain the functions that you will use.
  • Create the header files (.h) in one directory, with the prototypes of the functions you will use.
  • Compile all source files (* .c) into object files (* .o) without linking. To do that, we will use the gcc command:

$ gcc -c -Wall -Werror -Wextra *.c

Note that “* .c” matches all files in the current working directory with the extension “.c”.

2. Create static library

  • To create a static library or add additional object files to an existing static library, we have to use the GNU ar (archiver) program. We will use “libholberton” as an example of a library name:

$ ar -rc libholberton.a * .o

The ‘ar’ is the program used to archive the files. The ‘c’ flag tells the program to create a library if it doesn’t already exist. The ‘r’ flag tells the program to insert, replace or update the oldest files in the library, with the new object files.

  • After creating or modifying a file, it needs to be indexed. This index is later used by the compiler to speed up searching for symbols within the library and to ensure that the order of symbols in the library will not matter during compilation. The command used is ranlib:

$ ranlib libholberton.a

To list the names of the object files contained in the static library, we can use the ar command with the -t flag:

$ ar -t libholberton.a

How are static libraries used?

In the previous steps we have created a static library “libholberton.a” and it is ready to be used.

We write a program, for example:

#include “holberton.h”
#include <stdio.h>

int main(void)
{
printf(“My static library is working!”);
return (0);
}

In the header: “holberton.h” contains functions and function definitions that are used to tell the compiler how to call the functionality.

Then we can use the following command to create our final executable program using the following command:

$ gcc main.c -L. -lholberton -o quote

Description of the flags:
-L: specify the path to the given libraries (‘.’ Referring to the current directory)
-l: specifies the library name without the “lib” prefix and the “.a” suffix, because the linker attaches these parts back to the library name to create a filename to search for.

Now we can run the executable:

$ ./quote

If the library is working properly the output should be:

“My static library is working!”

That was my summary of static libraries in C. I hope you find it very useful.

--

--