Getting started with static libraries in C

Carlos Zuluaga
4 min readJul 15, 2019

--

Photo by Alex Block on Unsplash

What is a library and why use it?

The formal definition of a library is as follows.

A collection of pre-compiled and non-volatile routines used by programs. These routines, sometimes called modules, can include configuration data, documentation, message templates, subroutines, classes, values or type specifications.

In a nutshell, a library is a collection of functions (routines).

The standard C library contains routines for common operations, such as input/output or string handling. It has 24 header files that can be included in a programming project with a simple directive.

Some of the best known are <stdlib.h>, <stdio.h>, <string.h> and <math.h>. With a simple call, we can access functionalities that have already been created previously, but how do they work exactly?

How do they work?

There are two types of libraries: static and dynamic.

Static libraries (also known as archives) contain the object files of the function’s implementation with a table of contents in front giving the address of each name. Static libraries are created from object files using a librarian utility. One such programs is ar, for archive.

Figure from HelloACM

Functions in static libraries are joined to a program’s main module by a static linker at build time to produce an executable program.

The library’s object code is included after the program’s object code when the executable is created, for this reason, the executable is self-sufficient.

Like a static library, a dynamic library is a collection of functions with a table of contents. They are referenced at build time to give the executatble information about how they will eventually be used, but they aren’t used until run time.

The fundamental difference between static and dynamic libraries is that static libraries include the object code of the functions within the executable and dynamic libraries include a memory address where the system will load the library.

Dynamic libraries are part of the run-time environment. When a program is run, the run-time linker finds the dynamic libraries needed by the program, finds the addresses of the required functions, and assembles a runable image in memory.

How to create our own static library?

Suppose I want to create several programs that perform calculations of the area of a triangle, a circle, and a rectangle.
Instead of implementing them in each of my programs, it’s smarter to write each one once and reuse them when necessary.

So, the first step is to create our header file. C header files include functional prototypes, declarations (not definitions) of functions. Functional prototypes describe to the compiler each function’s parameters, allowing the compiler to confirm that the function is being called correctly.

areas.h

The second step is to create the implementation for our library, in this casecircle_area.c , rectangle_area.c and triangle_area.c .

Each function has been implemented in a separated file

The third step is to create a library object file that can be linked with programs that want to use our library code.

In order to do that we have to compile our implementation to get object code, using GCC we type the following command in the terminal.

gcc -c *.o

The basic tool used to create static libraries is a program called ar, for 'archiver'. This program can be used to create static libraries (which are actually archive files), modify object files in the static library, list the names of object files in the library, and so on.

To create the library we use the following command.

$ ar -rc libareas.a *.o

The flag -r is used for replacing older files and -c is used to tell the archiver to create an archive file.

Then we update the index table with the command ranlib.

ranlib makes a header in the library with the symbols of the object file contents.This helps the compiler to quickly reference symbols. A large library may have thousands of symbols meaning an index can significantly speed up finding references.

$ ranlib libareas.a

Now the library is ready to use. If we want to list the implementations we could use ar -t libareas.a or nm libareas.a.

How to use them?

By now we have our static library libareas created. To use it in a program we add the library’s name to the list of object file names given to the linker, using a special flag, normally -l.

$ gcc main.o -L . -l areas -o program

The compiler creates the program using the object file of our program and our library. The lib prefix and the .a is not mandatory. The flag -L receives the library’s path, in this case . (current directory).

Hope this article helps you!

--

--