Custom Libraries in C Part 1: Static Libraries (Linux):
What, how, why and when?
Something my parents inculcated in me since I was a child was the habit of reading, any book I wanted they would buy it for me, in fact, in my parent’s house I have a library full of, guess what? books, some half read, some old, some new, but all of them containing valuable information that at any time I can find organized, now, I can keep my books in a digital library with the use of my e-reader, but the library is always present; the same happens in the C programming language.
In C as in many programming languages, libraries are collections of precompiled functions that have been written to be reused by other programmers, let’s illustrate this with an example: you are developing an application that has repeated functions in different files, the developers realize this and build a library that contains these functions and thus save space and make your program more optimized, more understandable and portable.
Types of libraries in C:
In C there are static and dynamic libraries, dynamic libraries exist outside the program, in separate files and can be modified, the static libraries are added to the program at the time of compilation, so if you want to modify them you would have to recompile the program, in this blog we will focus on the last type of library.
How static libraries work:
Libraries are, in a few words, files that contain other object files, which are the files that result from the compilation of a source file, something like a zip of .o files without being compressed, in the linking stage of the compilation the symbols referenced in the headers are located, linked between them and the executable is produced.
How to create a static library in c?
First you have to obtain the object files, for this case, you are going to create a library called libhelloworld that contains the functions hello and world contained respectively in the source files hello.c and world.c, to obtain the gcc files you use the following command:
gcc -Wall -pedantic -Werror -Wextra -c hello.c world.c
This will generate the files hello.o and world.o.
Then the following command is used to create the library:
ar rc libhelloworld.a hello.o world.o
The library always has to start with lib, ar is the program that creates the library, the ‘c’ flag tells ar to create the library if it doesn’t already exist, and the ‘r’ flag tells it to replace older object files in the library, with the new object files, finally we get the library libhelloworld.a.
If we want to see which .o files are inside the library we can use the following command:
ar -t libhelloworld.a
How to use a custom static library?
To use it we only have to add the name of the library to the execution commands, suppose we want to compile my main.c that uses functions from the library we just created, for that we use the following command:
gcc main.c -L. -lhelloworld
This would generate an a.out executable that implements the library functions in the main.
So know you know it, what, how, why and when create and use a custom standard library in C.
If you want to read part 2 of this story, in wich we’ll learn about dynamic libraries, here is the link:
https://2348.medium.com/custom-libraries-in-c-part-2-dynamic-libraries-linux-b679bd14d0e9