Some background context..
A static library is a single collection of object code files (created during the compilation process) that speeds up the creation of a program’s executable file, in particular, during the linking phase. However, they make files really large because the entire library is linked to the executable file unlike dynamic libraries that are mapped into the process during runtime.
In C, the static library can be seen in the
/usr/lib/ directory of your Linux operating system. Static libraries are also known as archives, and I think that the naming has something to do with the definition of an archive —a collection of historical documents or records providing information.
Libraries have the
.a extension and
lib as a prefix before the library name so naturally it makes sense that the C library is
You can inspect a library by typing
ar -t <archive> into your terminal.
ar command creates, modifies, or extracts from archives.
-t flag displays a table listing the contents of the archive.
The library needed to accomplish such as task is the standard input/output library. See below for an example.
There are two key distinctions to make between header files and libraries.
- a header file has the declaration of the function — aka the “prototype”
- a library contains the definition of the function
During compilation the main function will call another function — aka subroutine and the compiler will look to the C standard library for the object code of said function/sub-routine(s) and link where appropriate so that the final executable is properly created.
Why are libraries important?
Libraries are an excellent tool for code reusability because they contain function modules that can be used by other functions, hence reusability. If you have multiple programs that use the same functions it’s best to put those related functions in a library to speed up the compilation process (assuming you’re using a static library) because looking through an indexed library is much faster than linking a program that has separate object files.
How do you create a static library?
The first step is to run our C-files through the GCC compiler to create the corresponding object code files ending with the
.o extension using the
* wildcard and pathname expansion to do the operation on all
Next, we use the
-c flag on
gcc to run the files through the compiler up to the linking stage.
gccall -c *.c
note: gccall is my custom alias for the following flags:
Next, use the
ar command to create an archive.
To create a static library named
scratch with all of the object files in our working directory, we would use the following command:
ar -rc libscratch.a *.o
This creates the static library named
libscratch.a and inserts the (copies of) object files inside of it.
-cflag specifies to create the library if it doesn’t already exist.
-rflag replaces old copies of object files with newer files if the file has been updated.
The next step is indexing, which is what speeds up linking and has the unique feature of being position independent in said library. To ensure indexing of a static library, use the following command —
ranlib. Even though
ranlibis embedded into the
ar command it is still good practice to run this command since some dependencies (such as not having up to date packages) can cause not using ranlib to create issues.
On another note you can use the command
nmto look at the contents inside of a library. This is similar to the ‘cat’ command in that it displays the contents inside of the file.
And there you have it, you’ve pretty much learned the TL;DR on static libraries. If you want to learn more I suggest looking through YouTube and StackOverflow.