Static C Libraries


Introduction

Before starting to talk about the subject matter, let us take a brief look at the compilation phases of a C program.
There are basically four phases:

  1. Pre-processing
  2. Compilation
  3. Assembly
  4. Linking

In this article, we will focus on the static libraries and their role in the linking phase of a program. But, first of all, let us define a library.

What is a library?

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 the same code over and over again, we can archive it and extract it from something that has already been written and optimized.

Why use libraries?

The main reason for writing or using a library is to allow code reusability, thus save considerable development time.
That is where the idea behind libraries comes from. We are reusing blocks of codes that have come from somewhere else.

What is a static library and how do they work?

A static library is a file containing a collection of object files (*.o for unix like systems) that are linked into the program during the linking phase of compilation.
The referenced code in the main program is included from static library into the resulting program after compilation process (Linking phase) and the program can run in stand alone mode (not library dependent on run-time).


How to Create a C Static Library on Unix like System

To create a static library, we need to specify to the compiler, that we want to compile all library codes (*.c) into object files (*.o)  without linking.
For this example the compiler is gcc.
gcc -Wall -pedantic -Werror -Wextra -c my_math_func.c print_alphabet.c
Flag description:
-Wall, 
-pedantic, -Werro and -Wextra: These aren't necessary but they are recommended to generate better code.
-c: Compile and assemble, but do not link.

Now we have 2 object files called:
my_math_func.o print_alphabet.o
We can now bundle all object files into one static library.
To create a static library or to add additional object files to an existing static library, we have to use the GNU ar (archiver) program:
ar -rc libname.a *.o

This command creates a static library named "libname.a" and puts copies of the object files "my_math_func.o" and "print_alphabet.o " in it.

Flag description:

-r Insert object files or replace existing object files in the library, with the new object files.

-c Create the library if it doesn't already exist.

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 will not matter during compilation.

Let's index it  using the ranlib command:

ranlib libname.a

Other way is using -s flag in ar command:

ar -rcs libname.a *.o

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

ar -t libname.a

How to use them?

We can use it in a program. This is done by adding the library's name to the object file(s) given to the linker. First, let us create a C source file that uses the above created static library, for example "main.c":
void print_alphabet(void);

int main(void)
{
        print_alphabet();
        return (0);
}
Now we can use gcc to create our final executable program:
gcc main.c -L. -lname -o main
Flag 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.

The output of our compiled program using static library will be:
$./main
abcdefghijklmnopqrstuvwxyz

Comentarios