The Breakdown on C Static Libraries
Why use them, how they work, how to create one, and how to use them.
First off, libraries are tools that are supplied by the complier. An example of a compiler would be “gcc”, short for GNU Compiler Collection.
If you’re unsure what gcc is, then please feel free to look at my article here, which explains the compiler’s process in depth. Otherwise, the four main parts of compilation are: Pre-processing, Compilation, Assembly, and Linking.
For Unix users, they could create and use their own libraries in addition to the many that have already been created. In actuality there are two kinds of libraries- static and shared (also known as dynamic). Dynamic libraries are not covered within this article, but static libraries will be explained in depth.
What is A Static Library and Why Make One?
Static libraries are basically collections of object files. These files are then linked, into what ever program they’ve been called in, during the linking phase of compilation. This just so happens to be the last stage.
A programmer might want to use or make libraries because it’s faster and generally more handy. This is because a library is a file that is made up of many different object files. This groups multiple object files into a single item, which doesn’t just make code easier to write, but causes the linking phase of compilation to be a lot faster.
Otherwise, linking would take longer because the object files wouldn’t be grouped together. The program would have to search through and open many different files on the disk instead of looking at just one, the library.
As a quick refresher, in order to create object files, one would need to use the flag “-c” with gcc. This stops the compilation process right before Linking, right after Assembly. This would be example of how it’s written out:
gcc -c example_file.c
How to Create A Static Library
The command “ar” is actually a tool that allows a static library to be created, as well as list and modify object files within it. The following is an example to create a static library called “exlib” and have it contain copies of the object files “ex_file” and “ex_func”:
ar rc exlib.a ex_file.o ex_func.o
“ar” actually stands for archiver which explains the “.a” file extension in “exlib.a” because a static library is actually an archive file.
The flags “r” and “c” were included in case if a library of the same name already existed. As this example would instead, have just added object files if they were missing or newer than already existing ones within that library.
To break it down further, the “r” flag told “ar” to replace the older object files while the “c” flag told “ar” to create the library if it didn’t already exist.
How to Use Static Libraries
Using static libraries is actually quite simple. When compiling a program to create an executable, just add the library. For example:
gcc main.c exlib.a
This would compile “main.c” with the library “exlib.a”. Since this example didn’t specify the executable file-name, to run the program one would just call:
./a.out
If the programmer wanted to specifify the executable name, then they would add the “-o” option with the name:
gcc main.c exlib.a -o ex_program
This way, the executable would be named“ex_program” and would be called like so:
./ex_program