C static libraries

Julian Mendez
3 min readOct 13, 2020

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.

A Library is a set of functions. It’s like a program, only it doesn’t have a main() function. It contains typical functions to be called from some other program or library. Any developer can create a new library.

Why use libraries?

One of the problems with developed programs, is that they tend to grow larger and larger, bringing up overall compilation and linking time to a large figure, and polluting out makefile, and the directory where we placed the source files.

It is this point where we start thinking about combining out source code into small units of related files, that can be managed with a separate makefile, possibly by a different programmer.

How they work?

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.

How to create them?

Before creating the library we must first compile the functions that we are going to include in the static library, for them we can use the following command in the bash:

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

Remember that this command it is gonna include all the files ending in .c in the current directory.

After use this command all the files ending in .c will be turned to into files ending in .o which it’s means that files now are ready for our library.

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. In order to create a static library, we can use a command like this:

ar -rc libname.a *.o

Remember that this command it is gonna include all the files ending in .o in the current directory.

The command above have created a new library called libname.a

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.

The command used to create or update the index is called 'ranlib', and is invoked as follows:

ranlib libname.a

Now the index is updated and is ready to use in any program that we are making.

How to use them?

If we need to use any of our functions we will need to compile the main.c file including our static library, the command is invoked as follows:

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

Note that we omitted the lib prefix and the .a suffix when mentioning the library on the link command.

In bash it will look something like this:

ubuntu@ip-172-61-63-244:~/directory$ gcc main.c -L. -lname -o alpha
ubuntu@ip-172-61-63-244:~/directory$ ./alpha
abcdefghijklmnopqrstuvwxyzubuntu
ubuntu@ip-172-61-63-244:~/directory$

This will create a program using object file main.c, and any symbols it requires from the name static library. Note that we omitted the lib prefix and the .a suffix when mentioning the library on the link command.

The linker attaches these parts back to the name of the library to create a name of a file to look for.

Note also the usage of the '-L' flag - this flag tells the linker that libraries might be found in the given directory ('.', refering to the current directory), in addition to the standard locations where the compiler looks for system libraries.

--

--