STATIC LIBRARIES IN C
To start, I’ll give a little description about what a librarie is. A library is a file that contains multiple object files, which can be used as a single entity in a link phase of a program. It is simply a collection of files of ordinary objects.
Its purpose is to be used by other programs, independent and simultaneously. In other words, a library is a file that the compiler can read, in it you will find instructions for the use of many different methods and functions
There are two kind of libraries in C: statics and dynamics. The static libraries are just collections of object files that are linked to the program during the linkage phase of compilation, and are not relevant at runtime, while the dynamics link when executing.
Thre are two big differences between both:
- Static libraries are included in the executable, while dynamic libraries are external files, so the size of the application (our executable) is greater in the first case than in the second.
- Dynamic libraries are independent files that can be invoked from any executable, so that their functionality can be shared by several executables. This means that only one copy of each library file (DLL) is needed in the System. This characteristic constitutes the main reason for its use.
How to create a library?
To create a static library using GCC we need to compile our library code into an object file so we tell GCC to do this using -c. For this explanation, the object files are goint to be all the files in directory that ends with .c
We are using -c option that is for the compiler doesn’t link the object files yet but instead creates counterpart object (.o) file for each source (.c) file, and we get:
Now that we have the object file(s), we can archive them and make a static library using ar.
This tells ar to create an archive (option c) and to insert the objects, replacing older files where needed (option r) .
Now we have creat our library.
Whenever files are added to a library, including the initial creation of the library , the library needs to be indexed, which is done with the command
ranlib makes a header in the library with the symbols of the object file contents.This helps the compiler to quickly reference symbols. A large library may have thousands of symbols meaning an index can significantly speed up finding references. This step is not strictly necesary.
If we want to see the files in our library we use the next command: ar -t
Or we can use nm tosee the symbols in our library:
Now let us use the static library by invoking it as part of the compilation and linking process when creating a program executable. Incase of gcc we use following flags to create static library:
- -L : Specifies the path to the library. Look in directory for library files.
- . : Represents the current working directory
- -l: Libraryname without lib prefix and extension
- holberton: Is the name of our library. Note that we omitted the “lib” prefix and “.a” extension. The linker attaches these parts back to the name of the library to create a name of a file to look for.
- -o quote: Says “name the executable file my_program”
Finally we run the program, and If everything worked out, the result will be an executable file quote that prints the following line that is contained in the libholberton.a static library: