C — Static libraries

Juan Camilo Gonzalez
5 min readJul 14, 2021

--

Description of a library

A library is a collection of objects that are made available for use by other programs. There Static Libraries and Dynamic Libraries. In this document you will find information about Static Libraries. The extension for static libraries is .a.

A library is a file containing several object files, that can be used as a single entity in a linking phase of a program (last one in the below picture). That phase is at the same time inside the compilation phase, and the libraries are not relevant during runtime. Normally the library is indexed, so it is easy to find functions and variables. 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.

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.

Anything linked in from the static library is compile directly into the final object. If you want to make changes to the static library, you need to recompile the linked binaries. It is best to put functions in a library to speed up the compilation of the program. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.

One of the advantages about using libraries is the fact that this libraries do no need to reside on the target platform. You can create a library to distribute it to other developers to use. If the library comes from a third party, you have access to the library, but not to the source code.

Since the beginning of C programming, there was a need for libraries, a place to store the commonly used functions in C for easy access and use by programmers using C. Creating a library tailored to your personal programming needs may save you a lot of time during compilation and programming.

How do static libraries work?

Static libraries are added during the linker phase of the compilation process (above). During the linker phase: the linker links access all the libraries to link functions to the program. Static libraries are merged with other static libraries to create an executable program. During the compilation of a program, the static library is called to execute the program.

Creating a library

The first thing to do to create a library is compile the source code for the static library into objects. For that, you use the gcc command.

gcc -c *.c

The C option tells the compiler (gcc) to compile the files (*.c) into object files (*.o), but not to link them.

Then, you are create a library. For that, you need to follow the next convention:

ar cr libtest.a file1.o file2.o

  • ar is the command to create a library
  • The ´c´ flag tells ar to create the library if it doesn't already exist.
  • The ´r´ flag tells it to replace older object files in the library, with the new object files.
  • The text lib before the name of the library (test) is always use to let the program know that “test” is the name of the library.
  • file1.o and file2.o are the object files copied inside the library.

After creating the library and, if you want to see the object files inside of it, use the ‘t’ option.

ar t libltest.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 (this will be better understood when we take a deeper look at the link process at the end of this tutorial).

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

ranlib libtest.a

One of the first things to do is create a .h include file to define prototypes for library functions. Also, the libraries should go inside the library directory.

To create a link with a static library, you must use the following convention:

gcc source.c -ltest — o outFile

  • -I <include directory>
  • -L <library>
  • Test is the name of the library.
  • The ´-I´ option is used to specify any non-standard include directories
  • The ´-L´ option is used to specify any non-standard library directories

Using a Static Library

After we creating the archive, it can be used it in a program. This is done by adding the library’s name to the list of object file names given to the linker, using a special flag, normally ' -l ’. Here is an example:

  • cc main.o -L. — ltest -o prog

This will create a program using object file "main.o", and any symbols it requires from the "test" static library. Note that the "lib" prefix and the ".a" suffix was ommited 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.

Sources

(Don´t go out without checking these amazing sources)

--

--