STATIC LIBRARIES

Carolina Hernandez Viveros
4 min readOct 12, 2020

Libraries are a package of one or more functions that we have compiled, they help us to develop our code in an agile and recursive way, the advantages of libraries are that we do not have to rewrite code, it saves us time when compiling .

There are two types of libraries, the dynamic libraries ones that allow the reuse of shared code, this means that the code is housed once in memory and is used by the rest of the applications or libraries and about which we will talk, which are the static libraries.

Static libraries or object libraries are a collection of object files (compiled) which contain the functions that we constantly use in our programs, all packaged in a single file with a .a extension which is complemented by the header file (.h file) , this library is copied into our program when we compile it and is included in the executable, so the size of the application (our executable) will be larger in the first case than in the second. This can be of great importance in very large applications, since the executable must be loaded into memory only once.

We must bear in mind that if modifications are made to any of the files of a static library, it is necessary to recompile all the executable that use it. During the execution of an executable, the static libraries that had intervened in its construction can be eliminated.

How to create them

  1. We must encode the files that contain the functions.

2. We obtain the object files (.o) from our source files (.c), using the -c option in the compiler.

3. Package the object files that will be integrated into the library (.a) by executing the ar command followed by the rc parameters, the flag c tells us that the library does not exist and will be created and the flag r inserts / replaces the object files to the library.

4. After the library is created, it is necessary to index it to speed up the search for symbols within it and ensure the order, we execute this with the ranlib command followed by the name of the library, then we use the ar command and the flag t followed by the name of the library to list the object files.

How to use them

  1. The headers that are files with the extension .h, these files contain the prototypes of the functions (the prototype contains the name of the function, the arguments and the return value), all the prototypes must go inside a block #ifndef with the name of the library like this NAMEFILE_H followed by a #define and #endif at the end of the block.
File basic.h

2. The compiled library, the compiler must know where the static libraries are and what they are. This is done by adding the name of the Library to the list of object file names given to the linker. Now, we compile our program with the compiler, and using the -L flag that serves to indicate the directories in which the libraries are located.

creating the executable file and compiling it

I share an interesting video about the differences between static and dynamic libraries.

--

--

Carolina Hernandez Viveros

Software development student at Holberton School, lover of science, technology, cooking, music, cats and nature. Also geek