Static Libraries in C
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. The first time a program we write reaches this state is normally when we look for a different way to manage our projects.
It is this point where we start thinking about combining out source code into small units of related files (Libraries), that can be managed with a separate makefile, possibly by a different programmer (for a multi-programmer project).
But, What is a Library?
A library is a file containing several object files, that can be used as a single entity in a linking phase of a program. These are collections of object files that are linked into the program during the linking phase of compilation for further use.
There are two types of libraries, Static libraries and Shared libraries; the main difference between both is how they are linked to the main program. For Static libraries the whole code is copied into the executable file after compilation, for Shared libraries only is copied the relative memory address to be referenced in runtime.
How to create Static Libraries?
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 libutil.a util_file.o util_net.o util_math.o
This command creates a static library named 'libutil.a' and puts copies of the object files "util_file.o", "util_net.o" and "util_math.o" in it. If the library file already exists, it has the object files added to it, or replaced, if they are newer than those inside the 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.
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 libutil.a
How to use Static Libraries?
After we created our archive, we want to use 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. -lutil -o prog
This will create a program using object file "main.o", and any symbols it requires from the "util" 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.
What is next?
If you want to learn more about Libraries in C you could read the source document of this article at https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html#what_is_a_library