Tutorial to create static and dynamic libraries in C

Ric Hincapie
The Startup
Published in
4 min readMay 5, 2020

--

Let’s start with a simple definition: a library is a collection of functions. Those functions are stored in object code format so they are executed very fast by the operating system.

It sounds simple, and it is in fact simple. The kind of simplicity that precedes a great dischard of power, because libraries are the nutshell of the very reason why humans are the most successful species on earth: collaboration.

We use libraries because they store functions that others before us have written and taken to an utmost state of reliability. A library archives a vast amount of work and talent ready to be deployed in our fingertips.

Libraries work by including them in the first part of our code with, for example, #include <stdio.h>. In the Linker stage of the compilation, the binary code from the called library functions is inserted, and the operating system executes them. There is a trusted path compiler follows to find libraries, which usually is/usr/lib and/lib.

In the above example, stdio.h file is a header file, where the functions contained by the library are declared so the compiler can match if those functions are being used correctly, like being inputted with the right data types. Keep in mind that the header does not contain the functions, it just declares them with functions prototypes. The header file references the real object files where the functions are stored and from where they are inserted into the program or called from within. The difference between inserting a function’s code or calling the function’s code is what differs static from dynamic libraries.

How to create and use a C static library?

That’s an easy task! Follow these steps and you will be done really quick.

1 — Create a header file in the same directory where you have the functions in C language and write their prototypes in different lines:

void Sum( int a, int b );int MyFunction( int n);

Keep in mind that a header file has its own structure.

2 — Compile all your .c function files like this:

gcc -c filename1.c filename2.c filename…n.c

Please remember that all the .c files must be in the current directory. The -c flag is asking the compiler to output only the object code file (“.o”).

3 — Use the command ar (for “archive”) to take all the object code files into one single file

ar rc -s my_library_name.a my_.o_file1 my_.o_file2 my_.o_file…n

In this command you ask arto: “r” insert the files members into the archive; “c” create the archive; and “-s” create or update the index file inside your library (which is mandatory to do).

You have created your own status library! Congrats!

4 — Now it is time to add the library’s name to the list of object file names given to the linker to work with. This is a delicate step. You need to inform the linker where your library is with the “-L” flag, then give it the path to the header file with the “-I” flag, and finally tell it what file to include with the “-l” flag. Here you can see an example:

gcc -I /path/to/new_library/header -L /path/to/new_library-l name_of_lib_without_prefix_lib_and_without_.a_

Note that when using the “-l” flag the compiler will automatically add the prefix “lib” and the sufix “.a”.

Dynamic libraries: a step forward in collaboration

A dynamic library has the same nature as a static one: it facilitates collaboration. It also makes it more efficient and updatable to collaborate.

Efficient because when using a dynamic library, in the Linker phase of the compilation the function’s object code is not inserted into the program’s code, but it’s address is referenced, so in runtime the operating system will search into that direction and execute the function. If you count the number of times a big program has to call library functions, and for each call it adds some new bytes into the executable file, you sure can guess why not including the functions’ codes is better: it makes the executable smaller.

Updatable because as it is true that libraries are utmost reliable, they are always ways to improve them or to include new functionalities. So, let’s say you used a library in your program and that library got updated so some functions you used work better (fixed bugs, more efficient, etc.). If you want to use the updated library, you would have to compile the program again, which in small scale is not a problem, but our digital world is made for scalability and once you achieve it, you better have automatized solutions for this kind of situation.

How to create and use a dynamic library?

1––Take the .C function files that will be part of you library together.

2–– Compile them all to object code -c with the -fPIC flag. This flag stands for “Position Independent Code”, which means that

“the generated machine code is not dependent on being located at a specific address in order to work”. ¹

3–– Create a dynamic library file with .so extension and include into it all the object code files previously compiled.

4–– Install the mylibrary.so in one of the trusted directories for libraries. Super user status is required.

5–– Run ldconfig command, which

“creates the necessary links and cache to the most recent shared libraries found in the trusted directories” ²

6-– Include your new installed library with #include <mylibrary.h> and you’re good to go!

I invite you to check out this wiki, where you will find a table with the most used C libraries. Getting to know these tools will sky rocket your programming skills.

References:

¹ https://stackoverflow.com/questions/5311515/gcc-fpic-option

² https://www.lifewire.com/ldconfig-linux-command-4093742

--

--