Do you know about Dynamic Libraries vs. Static Libraries?

Arquimedes
4 min readSep 7, 2020

First thing, first: What is a Library ?

A Library is simply a collection of functions which can be added to your application and the functions called as necessary, just like any other functions in the application. More precisely any object, not only functions can be stored in a library, but the vast majority of libraries only contain functions.

The format of a library varies with the operating system and compiler one is using.

For example, in the Unix and Linux operating systems, a library consists of one or more object files, which consist of object code that is usually the output of a compiler (if the source language is C or something similar) or an assembler (if the source language is assembly language).

Dynamic Libraries in C

The use of dynamic objects means leaving the binding of these objects pending in the assembly of the application. When the application is running, and only then, the (dynamic) link with the objects contained in the library occurs.

The creation of dynamic libraries is the responsibility of the linker or assembler, although it is also possible to indicate to the compiler the necessary options for assembly and thus, it will be he who is in charge of passing them to the assembler.

Creating a Dynamic Library in C

When creating a dynamic object it is necessary that said object code be independent of the position, to get this type of code you must specify to the compiler the option -fPIC (Position Independent Code). This flag must be indicated both in the compilation and in the assembly of the library. To mount the objects it is also necessary to indicate the -shared option so that the result is a ‘shareable’ object file.

$ gcc -g -fPIC -Wall *.c -shared -o libholberton.so

Flags:

  • g: Debugging information
  • -fPIC: Start the position independent code, that is a requirement for dynamic libraries.
  • -Wall: enables all compiler’s warning messages. This option should always be used, in order to generate better code.
  • -shared: This allow the dynamic library be created as a shared. The .so (shared object) extension.
  • -o: Create the output file, in this case is libholberton.so
adding the library libholberton.so

Using this flags + gcc command, we are compiling all the C files in the current directory and created a shared library called libholberton.so that now ready in the machine.

Using Dynamic Libraries

We can use our Dynamic library with a main file (main.c)

After we create de library libholberton.suse the file main.c

$ gcc -Wall -L. main.c -lholberton -o len

  • -L: after the library name telling the linker that the library might be found in the current directory.
  • main.c: This is want to use the references on the dynamic library, and after execute the program.
  • -lholberton: The dynamic library named with the extension .so.
  • -o: This create the file executable: len.

When using a library, the compiler first looks for a dynamic version (.so),
if it can’t find it then look for the static version. If you have both versions
of a library and you want to use the static version, you must indicate to the assembler the flag -static.

When a program uses dynamic libraries, the system needs to locate them in
runtime (unlike with static libraries). The places where a program looks for the dynamic libraries are the following (in this order):

  • In the directories of the variable LD_LIBRARY_PATH

$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

  • In the ld.so.cache file
  • In the / usr / lib and / lib directories

$ export LD_LIBRARY_PATH=$HOME/lib:$LD_LIBRARY_PATH

Dynamic Libraries vs. Static Libraries

Dynamic vs Static

Dynamic

Advantage:

  • One of the great advantages of using dynamic libraries, apart from having files smaller executables, is that we can modify the implementation of the libraries without having to recompile the programs.
  • Only one copy of the shared library is kept in memory, making it much faster to compile programs and significantly reducing the size of the executable program.
  • Dynamic linking load time will be reduced if the shared library code is already present in memory.

Disadvantage:

  • Slower execution time compared to static libraries
  • Potential compatibility issues if a library is changed without recompiling the library into memory

Static

Advantage:

  • Speed
  • All the code to execute the file is in one executable file, with little to virtually zero compatibility issues

Disadvantage:

  • Every program generated must contain copies of exactly the same common system library functions.
  • In terms of both physical memory and disk-space usage, it is much more efficient to load the system libraries into memory only once. Dynamic linking allows this single loading to happen.

--

--

Arquimedes

Beginner Programar, social comunicator. Love pop music, family and friends.