Dynamic vs Static Libraries

Hello there, in this blog we are going to talk about C libraries again, but this time I’m going to do more emphasis in the Dynamic or Shared libraries.

Libraries are sets of objects or collections of pre-compiled codes that can be used or reused in different programs, and they simplify the work for the programmers because makes easier to link commonly used programs in our projects. This set of objects are linked to the program in the linking phase of the compilation process, but it works differently depending on which type of library we are using, there are two types of libraries: Dynamic or Static.

The compilation process has four steps:

> Pre-processor phase > Compiler phase > Assembler phase > Linker phase.

If you want to look a little deeper about this, please check this link: Compilation process of a C Code.

Static Libraries:

At the compilation process, they are copied into the executable file by the linker. For a better understanding on this topic, please visit my previous blog in this link Basics of Static Libraries.

Advantages:
- Run time is faster because the code of the library is inside it, but loading the program into the memory take a littel bit more time.
- Compatibility is less probable to be an issue since all the code is in the executable file.
Drawbacks:
- When changes are made in the libraries the programs must be recompiled in order to get the updates.
- Static Libraries create much bigger programs, because the external programs are built in the executable file, increasing the size.

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

How to use Static Libraries:

Basically, to create a Static library we create .o objects files from .c files with gcc -c [files.c] command, then create the library with ar -rc lib[name].a [files.o] command, finally you can use the libraries with gcc [files.c] -L[directory] -l[libraryname] -o [outputfilename] command. If you want to see more details on how to use Static Libraries, please visit my previous blog to see more information — Basics of Static Libraries.

Dynamic or Shared Libraries:

This kind of libraries are linked to the programs in two stages:

  • During compilation time, the linker verifies that all symbols (functions, variables, etc) that the program requires exists in the shared libraries.
  • When the program is executed, there is a program called ‘Dynamic loader’ checks which shared libraries were linked previously, then load them in the memory, and attaches them the copy of the program loaded in the memory.

They are not included in the executable code itself, they are attached in the memory when the program is executed, this process is done

Advantages:
- Size of executable files is smaller, because the the library is loaded in the memory, and can be used by different programs.
- If we update the library it is not needed to recompile the program
Drawbacks:
- The dynamic loading step makes launching slower, but run time is faster because shared library code is in the memory.
- Compatibility: The programs won’t work if the library are removed from the hosting system.

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

How to use Dynamic Libraries:

The creation of a shared library is like creating a static library, first we compile objects files, then insert them in a library file. But there are two main differences.

  • PIC of Position Independent Code:
    As we don’t know where the objects files in the library will be inserted in the memory and different programs can use them, we need relative addresses of the objects, we can do that in most Linux systems with gcc fPIC -c [objectfiles] command. In the image below, you can see that using the command will create the objects files.

gcc -fPIC -c [objectsfiles]

Creating objects files.
  • Shared Library Creation:
    The dynamics libraries are not the final executable file, but a file in a format that depends on the specific architecture for which it is being created. For most Linux operating systems we do this with the command:

gcc -shared -o lib[nameoflibrary].so [PICobjectsfiles.o]

Creating library file

In the example, we use the flag -shared -o that is used for creating Dynamic Libraries. And finally, we get the lib[name].so file, in this case, liball.so file. As we see in the previous blog, to check the functions in the library, we can use the following command:

nm -D lib[nameoflibrary].so

checking liball.so
  • Using Shared Library:

For using the Shared library we just create, the compilation process is like the static libraries, so we take the program in which we want to link the library and use the command:

gcc [programfile] -L[directory] l[nameoflibrary] -o [executable]

This way the linker will scan the shared library we have in our current directory, and verify symbols in the program (functions, variables, etc), but as explained before, the objects will not be added to the executable file.

Then if we want to execute our program (run time), we must tell the system’s dynamic loader where is located the shared library. By default, it searches for the libraries in specified directories such as /lib or /usr/lib, but the first time we create a shared library that doesn’t belong to the system, we use $LD_LIBRARY_PATH variable, which is an environmental variable that must contain the path in which our library is stored.

So, the first step for this is to check if the variable exists:

echo $LD_LIBRARY_PATH

If it doesn’t exist we must create with the following command:

export LD_LIBRARY_PATH=[absolute_path_directory]

After this, we use ldd this program print shared object dependencies of the files.

ldd [file]

In our case, we use the library file.

ldd liball.so

We just created a shared library called liball.so, and as you can see there is the memory address where it is stored.

Let’s see another example, we have a file that uses a dynamic library function, this programs name is “0-main.c” and we want to use our library so we use the compilation file and then check with the “ldd” command.

So as you can see, we have de path where the library lives, which in this example is the current directory.

In conclusion, you can use Static or Dynamic libraries depending on the needs you have, in this blog you can find some advantages and disadvantages of each one so it can help you to make better decisions while making your projects.