C static libraries

Agustin Otegui
5 min readMar 7, 2021

On this blog I will be writing about:

  • What are static libraries in C?
  • How they work?
  • How to create one?
  • How to use them?

What are static libraries in C and how they work?

Before entering into static libraries, first I want to define what are libraries in C. A library is a collection of code routines such as functions or variables, that can be called upon when building our program. This allow us to call this code, which has already been written and optimized whenever we want, instead of rewriting it.

Basically, we have 2 types of libraries:

- Static libraries

- Shared or dynamic libraries

During this blog, we will focus only in static libraries. A static library is a file that contains object files (*.o), which are going to be link to the program when we run the linking phase of the compilation process.

As shown in the above image, when we compile a program, the compiler generates an object code from a source code. After generating the object code, the compiler invokes the linker. The role of the linker is to copy the code of the library to our object file.

Basically, static libraries are just a collection of object files that are merged by the linker with another object file to form a final executable.

Conventionally, they start with “lib” and end with “.a” or “.lib” (depending on your platform).

How to create one?

To create a static library, we first need to specify to the compiler (in our case is going to be GCC) which “*.c” files we would like to add to our library. After this, we would have to compile these files into object files (*.o) without linking them. To manage this step, we have to add the flag -c to the gcc.

$ gcc -c *.c

-c: Compile and assemble, but do not link.

Please note that “*.c” embraces all files in the current working directory with the “.c” extension.

To have a better understanding, let’s see an example:

We create 2 files which are called “add.c” and “mul.c”. They respectively make addition and multiplication of 2 integers. Also, we have a header file called “header.h”, which contains the prototype of these functions.

We start the process by compiling “add.c” and “mul.c” up to object code, without linking them. To do so, we run gcc -c *.c

The picture below shows the output generated after using the command.

Once we have the object files, we can create a static library and add these 2 files in it. In order to do this, we have to use the GNU “ar” (archiver) program command.

$ ar -rc libname.a *.o

This command creates a static library named “libname.a” and puts copies of the object files “add.o” and “mul.o” in it. The ‘c’ flag tells ar to create the library if it doesn’t already exist, while the ‘r’ flag tells it to insert object files or replace existing object files in the library, with the new object files.

After the library 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 will not matter during compilation. There are two ways to create or update the index. The first one is, by using the command ranlib.

$ ranlib libname.a

The second one is, by adding an extra flag (-s) to the “ar” command like this:

$ ar -rcs libname.a *.o

The picture below shows the execution of these commands on our example.

In order to list the names of the object files in our library, we can use the ar command with -t flag.

How to use them?

Now that our static library “libname.a” was created, it is ready to be used. For this purpose, we need to add the library’s name to the “.c” file, where we what to use one of our functions. In order to include our library properly, we need to add a “# include” followed by the name of the library between comas (“”), as that library was created locally. We can have a better understanding on the following picture.

Now we can use the command below to create our final executable program:

$ gcc main.c -L. -lname -o main

This will create a program using the object file “main.o”, and any symbols it requires from the “name” static library.

Flags description:
-L : Specifies 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 name of the library to create a name of a file to look for.
All we have to do now is to run our program.

I hope you found this blog useful.

All the best.

Agustin

--

--