Static Libraries in C
What are C libraries?
One of the tools that compilers supply C programmers. A library file contains a collection of functions and declarations for use by other programs and programmers.
How may they be used? A team of engineers calculating the tension of a bridge may develop their own library of maths functions to complete common tasks.
Why use libraries in C? As a programmer, you may find yourself using the same function or functions repeatedly. On this case, is better put this function(s) into a library to speed up the program compilation. C libraries store files in object code; during the linking phase of the compilation process ((Compilation Process) <--- HOW DO STATIC LIBRARIES WORK) files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.
Libraries make life easier
Libraries come in one of two flovars: DYNAMIC (Dynamic libraries are shared libraries with specific functions launched during the execution of a program and contribute to “reduced memory consumption”(techopedia.com). Dynamic libraries are linked in two stages. Static libraries produce object files and standalone executable files (wikipedia.org). These libraries can be linked to a program without recompiling the code.); STATIC (Static libraries date back to the creation of C itself. Static libraries house a collection of object (.o) files.)
Object files are products of the compilation phase and comprised mostly of machine code, but also contains information that allows the linkers to see what functions and global objects are located within the program.
Drawbacks of Static Libraries
For all of a static library’s efficiencies, it comes at a price. Because all object files within a static library are linked into the program during compilation, the resulting executables can get quite large. One can think of static library archives as similar to a zip file, but without the compression.
Secondly, if the library code is to be updated, a programmer must recompile their program into a new executable and every program that uses that particular library contains a copy in its executable.
How to Create Static libraries
Static libraries have the .a format. To create static libraries, we use a command called ar for archiver.
The first step is determining which functions we would like to include in our library and preparing them for the archiver — I like to place all the functions I like to use in a single directory. Next, we will compile all of our .c source files into .o object files by tagging on the -c flag:
$ gcc -c *.c
Once this command is entered, the corresponding .o files will be present in the directory. We are now ready to create our library file!
Next, we will take all of our .o files and package them into a single .a file.
For a full list of the capabilities of the arcommand, take a look at its man page (man ar) — its able to create static libraries, modify object files in the static library, list the names of object files in the library, and more.
Let’s suppose we will call our static library libsuper.a. We now have our necessary .ofiles. We will run the command:
ar -rc libsuper.a *.o
The -r flag tells the arcommand to create a library if it doesn’t already exist and replaces older existing object files in the library.
The -c flag tells the arcommand to create the library if it doesn’t exist.
We’ve created our static C library!
Now it’s time to employ our shiny new library.
Before we do so, let’s look at the linking option in the (lengthy) man page for gcc ( man gcc).
We specify the library to the compiler with -lname where name is the filename of the library without the prefix lib. Earlier, we created the library libsuper.a. We also specify the location for our libraries, namely the current directory using the flag -L.
Our final command to compile our program (we’ll name it prog in this example) is:
gcc main.c -L -lsuper prog
We have created a functioning static library.
I hope this article helped you become more than familiar with static libraries and C libraries in general. They are very useful tools in programming in C and C++. Since the beginning of C programming, there was a need for libraries, a place to store the commonly used functions in C for easy access and use by programmers using C. Creating a library tailored to your personal programming needs may save you a lot of time during compilation and programming.