C static libraries
What are they?
In order to complete a program we may need different object files, which are obtained when the source code is compiled. So when the linker is going to generate an executable file, it tries to resolve the referenced symbols, like locating in which object files they are defined and links them together. Here is where libraries come in handy.
A library is a file that contains several object files, and can be used as a collective in the linking phase of compilation. Since libraries can be indexed, they become a useful tool to organize the object files for a program, also there shall be fewer files to look for and open, thus making the process faster. Because object files are not needed to run the program itself, it is beneficial to link them all together with the usage of a library. Also, static libraries allow users to link programs without having to recompile its code.
How to create them?
The basic command to create a static library is 'ar', that stands for archiver. It is a GNU program that can create, modify, and extract from archives. The format of the libraries is the same as the archive files.
$ ar rc libtest.a file1.o file2.o file3.o
In the previous example, we used 'ar' to create the library called libtest.a (notice the prefix lib for library and the extension .a for archive), containing the following object files (notice that you could add multiple ones in the same command line). With the 'c' flag we can create the library if it doesn't already exist. On the other hand, the 'r' flag is used to replace older object files in the library, with the new ones. Another useful option for 'ar' is '-t', which displays the list of object files that a given library contains. Also, if you wish to list the symbols from object files within a library, you can use the 'nm' command as follows:
$ nm libtest.a
As mentioned before, libraries can be indexed, which can be useful for the compiler to speed up the processing of the symbols inside the library, and to make sure that the order of such symbols won't affect the compilation process. The index lists each symbol defined by a member of an archive that is a relocatable object file. In most cases the archiver already takes care of the index. However, the command 'ranlib' can be used to create or update the index of the library as shown in the following example.
$ ranlib libtest.a
How to use them?
Once the library is ready, you can add it to the list of object files given to the linker using the flag '-l'. We can see the implementation in the next example:
$ gcc main.o -L. -ltest -o main
Here we created an executable program called main, using the object file main.o and any symbols required from the test library (notice the omission of the prefix and the extension when referencing the library on the command line). The linker attaches these parts back to the name of the library to create a name of a file to look for. The '-L' flag tells the linker that libraries can be found in the given directory (in this case '.', that refers to the current directory), in addition to the standard location where the compiler looks for system libraries.
Resources:
https://tldp.org/HOWTO/Program-Library-HOWTO/static-libraries.html
http://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html#what_is_a_library
https://medium.com/@tomasmontoya123/about-c-static-libraries-9c5e24b53014