Static and Shared libraries in C


Libraries is a basic concept that one must understand fully in order to operate the C programming language. In order to accomplish this we must keep in mind there are two distinct types of libraries: static and shared. This article is meant to provide you with a solid base -explained easily- that will help you grasp the concept of how both types of libraries work, when to use each one, and how to create them.

Libraries in C (and any other language, really) determine how a program will function. Every program needs a library to know how to operate correctly and to tell it what each function the programmer wrote will do. Can you imagine writing all functions from scratch for every program you write? Neither do I, and that’s the beauty of libraries, they will help you collaborate with other programmers and you will be able to code more easily.

Before we go any further and get into more technical stuff we need to have some basic concepts ready and they are the following:

Library: collection of pre-compiled functions, symbols, and objects that a program calls upon when executed or compiled.

Static Library: collection of symbols that will be called when the program is compiled and will have no use afterwards.

Shared Library: collection of symbols that will be called every time the program is executed since it will not get hard-coded during compilation.

How does a static library work?

A static library will be added to your program during the compilation process, more specifically during the linking stage. Simply put a static library is only used to build a program not to run it. These are stored natively on your operating system and are called upon only when compiling.

An advantage to static libraries is the fact that when you run your program it can be a little faster due to it not having to look around for instructions on how to run its processes, but the downsize is your file can increase its size significantly when you are working on large projects.

Creating a Static library:

To create a static library you can use the ‘ar’ command. It is very simple to use and should be paired with a couple options which are ‘r’and ‘c’. What these mean is that the ‘ar’ command will now create the library if it wasn’t already there, if it was then it will just update it and its contents to what you are now creating.

This action can be broken down into the following parts: the main command, the options for the command, the name of the library to be created and finally the files to be included. That last part can be a little confusing because of the sintax but it is only telling the program to include all files ending with the .o extension (this extension is for files which have been compiled by GCC but not linked) since they are the ones that need to be included in a library.
How does a shared library work?

A shared library is the complete opposite of a static library, while it also consists of a set of functions, symbols, objects that a program uses to operate properly the main difference comes in the build up and the execution. A shared library will only be called upon when the program is running and it is not compiled with the program in the linking stage. What this means is basically the shared library is a resource the program needs to find in order for it to know what to do with its contents.

A shared library also has its advantage point. The size of the compiled file will be significantly lesser than the one that has a static library meaning it will take up less resources to run. Now, you may be thinking that it will also run slower but the difference in performance is not something significant that you must take into account. This type of library also has the advantage that it makes collaboration between programmers simpler and since only one copy of the library is kept in a system the booting stage of the program is much quicker.

Creating a Shared library:

The creation of a shared library shares the same principle of the static library: compile object files (the ones with the ‘.o’ extension that have not been linked) and then insert them into the library files; but in practice the procedure and the command is quite different.

First, we need to compile the program with a specific set of flags that will will compile for something called ‘Position Independent Code’. These flags are most commonly '-fPIC'or '-fpic' . These flags are used to compile the base ‘*.c’ files so they are suitable to be used in a shared library.

Second, since this type of library is not represented in an archive (.a) type of file it must have a structure specific for the architecture for which it is being created. This means we must use the ‘-shared’ flag in order to finish the process.

How do you use a library?

The magic is all in the compilation process. As you have previously seen the compiler is essential to building your libraries and now you will see it is also essential when you want to use them.

In order to call your newly created libraries you mus use your compiler with the ‘-L’ flag, signaling that it needs to (L)ink the library you specify next. Remember the files that the libraries contain end with ‘.o’? This means that they will now be carried on to the linking stage.

The sintax is the following:

gcc main.o -L .-librarytocall

This was a basic intro to libraries and what they consist of. Keep in mind this is a very open world of possibilities for when you can use each one and to always investigate and test the best option for you and your project.

Have a good one!


Comments 0