C — STATIC LIBRARIES
What is a library?
Libraries are a simple and versatile way to modularize and reuse code. A library is essentially a set of functions and/or procedures. Libraries allow encapsulating functionality, which may then be available in different programs. For this encapsulated functionality to be used, the program that requires it must be in charge of linking the library that contains it. Libraries can be classified into two large families: Static Libraries and Dynamic Libraries.
Let´s talk about Static Libraries
A Static Library is a series of object files (.o files) that have been archived together. This object files are linked when compiling a program, during the linking phase of compilation. In Linux you will see this libraries like .a files and in Windows like .lib files.
Creating a Static Library
- First we need to create the object files (.o) from our source code (.c). We can do this with the -c option in Linux gcc. The line showed below will create the object files for all the .c files in the current directory.
So if we have the next .c files in the current directory:
after we pass gcc -c *.c we are going to have:
2. Create the static library (.a). We can do this with a program called “ar”, for 'archiver'. The next line will create a static library named “libmylibrary.a”:
The output of this step is static library “libmylibrary.a2 which contain all the object files created in the previous step. The flag “r”of the command “ar” means the .a file will be updated if it already exists, and the flag “c” means that the archive will be created without any feedback.
We must always name our archives lib<something>.a. If we don’t name them this way, our compiler will have problems tracking them down.
It is important to index the file after creating it. Subsequently, the compiler uses this index to speed up the search for symbols within the library and to ensure that the order of the symbols in the library will not matter during compilation. The command used to create or update the index is called “ranlib” and is invoked as follows:
Using a static library
To use a library (that is, compile against it), we must have:
- The headers. The headers are the files with the extension .h that contain the “declarations” (also called “prototypes”) of the functions. A declaration shows the function name, its arguments and return value, but not the entire associated implementation. They are invoked through the ‘’preprocessor directive’’ (they are so named because it is done in the ‘’preprocessing’’ stage of the compiler) #include, which performs a simple substitution. The compiler must be able to locate the headers. In Linux gcc, the -I argument is used, such as in “-I/usr/local/include”
- The compiled library. The compiler must know where the static libraries are. In Linux gcc, the -L argument is used to define the path, and -l is used for the library name without the “lib” prefix and without the “.a” suffix.
To use our library 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”. Let’s see an example:
This will create a program using object file “main.o”, and any symbols it requires from the “mylibrary” 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.