Basics of Static Libraries

David Peralta
5 min readJul 14, 2019

Welcome again, today we are going to talk about static libraries, these tools are very powerful when you are creating C Programs, so we are going to cover some of the basics here.

But first for a better understanding is important that you have some knowledge about the compilation process, if you don’t, please don’t hesitate to look at my previous blog about this topic in this link.

Now, What are Static Libraries? they basically are a set of objects code files, objects that can be used by other programs during the linking phase of compilation. You may notice that in some of our C programs we use functions that aren’t defined in the code itself, but if we want to use them we need to include header files with #includeoption. Then Static Libraries are the collection of functions that those headers files contain, and at the compilation process, they are copied into the executable file by the linker.

HowTo — Static library [video] — https://www.youtube.com/watch?v=eW5he5uFBNM

Here you can see a program source code and the libraries. When compiling the linker take both binary codes and link them together in one executable file. The resultant file can be executed independently from the Static Library.

The use of Static Libraries has some advantages, like reusability, because while coding, we can use the same function modules contained in the libraries on different programs. Also when the resulting program is executed it can make multiple calls to the functions very quick because all is inside the same file. Besides at the linking phase, we have fewer files to look and open, which speeds up the process. That’s why we use Static Libraries.

There are some disadvantages, for example, the executable files are bigger because it contains the libraries code, also when updating the libraries the programs must be recompiled in order to have those updates. On the other hand, we have the Dynamic or shared libraries, those libraries are not in the code itself, they are attached in the memory when the program is executed, but this is a subject we are not going to explain here as we are talking specifically about the Static Libraries.

In the /usr/lib/ directory on most Linux Operating Systems we can find the C Static Library which is contained in the libc.a file. As you can see Static Libraries files have an.a extension and they begin with the lib prefix before the name as a standard, I will create thelibholberton.a library as an example so you can know the process.

Creating Static Libraries:

Let’s suppose you have created a set of programs and want to make them a Static library, the first thing we must do is create the object files, so having the source C code files in your directory you should use the gcccommand with the -c option, which compiles the source files but not links them, and creates a resulting .o extension file, which is the standard extension for the objects files.

gcc -c [file.c]

Set of .c and .o files after compiling with gcc -c option.

Here you can see a set of ‘.c’ and ‘.o’ files, I used gcc -c *.c command to compile the C files, so the objects were created.

Next step is creating the Static Library using ar or ‘archiver’ command followed by the list of names of object files in the library, in order to create the library I used the command ar -rc libholberton.a *.o .

ar -rc lib[name].a

This command creates the static library with the given name ‘holberton’ and puts copies of the object files in it. The -c option makes arcreate the library if it does not exist already and the -r option replaces older object files by new ones if needed.

Verify the libholberton.a Static Library.

If you want to check the objects that are in a certain library you will use the command ar -t as you can see in the previous image example. The -t flag display a table listing the contents of ‘archive’.

ar -t lib[name].a

Index: In some systems the ar program takes care of the indexing. The index is used by the compiler when compiling programs, so it speeds up symbol-looking, a large library may have thousands of symbols meaning an index can significantly make faster finding references.

Nevertheless, the command ranlib actually is used to index or re-generate index. Also there is the command nm -s or nm -- print-armap to list the index of a library.

If you followed the previous process, congratulations, you have your Static library ready to be used in any compilation process of a C file.

Using Static Libraries:

Here we have a very basic program which prints ‘hi’ followed by a new line, it is in the 1-main.c file and we need to compile using the static library in order to use my own _putchar function, as you can see the prototype is declared at the beginning of the file.

So the command for compilation will be:

gcc 1-main.c -L. -lholberton -o putch

gcc [file.c] -L[directory] -l[libraryname] -o [outputexectutablefilename]

Basically what here we added the library’s name using the flag -l so we omitted the ‘lib’ prefix and the ‘.a’ extension when mentioning the library to the command. Also, we used -L flag followed by a ‘.’ referring to the current directory. Both flags tell the linker that the library might be found in the current directory and the name of it.

That’s it! now we have an executable file which works independently. I hope this information is helpful to you.

--

--