To Share or Not Share; Static and Dynamic Libraries

Brandyn Reindel
5 min readSep 17, 2019

--

In programming, a library is a collection of precompiled routines that a program can use. The routines, sometimes called modules, are stored in object format. Libraries are used for storing frequently used routines saving the user the trouble of having to explicitly link them to every program that uses them. During compilation the linker will automatically look in libraries for routines that it does not find elsewhere.

Libraries importance in programming is a direct reflection of it’s usefulness. Because if a chunk of code is useful to multiple programmers or programs it can be stored in a library, making it easily reusable. For example let’s take a look at C; because the C language supports only the most basic features that it needs. C does not contain input-output (I/O) functions to read from the keyboard and write to the screen. Anything that extends beyond the basics must be written by a programmer, and can be stored in a library such as stdio.h which can be accessed by all programmers.

You have the ability to create your own libraries, either static or dynamic (also referred to as shared) . By doing so, you can split your program into reusable modules. This modular approach not only makes it easy to include the same code in multiple programs, but it also makes for shorter program files which are easier to read, test and debug.

To use a module or function that is located in the header file of a standard library, or a library you have created, in your own program you have to use #include followed by the header file name of the corresponding library at the beginning of your program. For the use of standard libraries, you would put the name of the library’s header file between less-than( < ) and greater-than signs ( > ). For libraries you have created yourself, you put the header file name between double quotes. The following is an example of the proper syntax:

#include <stdio.h>

#include “mylibrary.h”

As I mentioned before libraries can be either static or dynamic, allow me to explain this further and give you a better understanding of the these two types of libraries.

Static Libraries

Static libraries are the most the most straight forward way of using a library function by linking the object files from the library directly to your final executable program. Static libraries have no external dependencies and the library remains unchanged unless the program is recompiled. To create a static library you can follow these steps:

Step 1: Compile your Library code(.c files) into object code

$ gcc -c *.c

Step 2: Create a Static Library from all the object or “.o” files using the command ar

$ ar -rc libholberton.a *.o

Step 3: The next step is to create index for the library using ranlib command followed by your library’s name which must start with “lib”. ranlib makes a header in the library with the symbols of the object file contents. For example: $ ranlib libholberton

Step 4: Now we have a static library, libholberton.a let us use it by invoking it as part of the compilation and linking process when creating a program executable. For gcc we use following flags to create static library:

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

Advantages of a static library are:

  • Static libraries are not required at runtime, so you do not need to include them when you distribute your executable.
  • At compile time, linking to a static library is generally faster than linking to individual source files.

Disadvantages are:

  • If the library code is updated (say, to fix a bug) you have to recompile your program into a new executable.
  • Every program in the system that uses that library contains a copy in its executable. This is very inefficient (and a pain if you find a bug and have to recompile, as per point one).

Dynamic Libraries

Dynamic libraries is loaded dynamically at runtime for each application that requires it. Linking does not require the code to be copied, it is done simply by placing the name of the library in the binary file. The linking occurs when the program is run and, both the binary file and library are in memory. The steps to create a dynamic library are somewhat similar to creating a static library:

Step 1: Compile all the C files in your current directory using the below command

gcc -Wall -Werror -c -fPIC *.c -o dynamic.o

Step 2: Now we have an object code dynamic.o in our current directory. We need to convert this into a dynamic(shared) object file which can be linked with other objects to form an executable. This is accomplished using the following command.

$ gcc -shared -o dynLibrary.so dynamic.o

The -shared option creates a shared object(.so) which can be linked with other objects to form an executable. The .so suffix stands for Shared Object, and this is convention for Dynamic Library names.

Step 3: This creates a Dynamic Library file called dynLibrary.so in your current directory. Since a dynamic library is linked during runtime, we need to make this file available during runtime. The dynamic linker searches standard paths available and also searches in system cache for dynamic libraries. So our next step is to add our working directory to the path so that the linker can find our library file. The below command is used for this.

$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/my_path/

If you do not want to export your current path to LD_LIBRARY_PATH for the dynamic library to work, you can move the library to /usr/local/lib because ‘/usr/local/lib’ is already a path specified in the ‘LD_LIBRARY_PATH’ environment variable.

To do this you need to run “ldconfig” on the directory you moved it to. This will add our library to a cache which is searched through at runtime when a program looks for a shared library:

$ ldconfig /usr/local/lib

Step 4: Our dynamic library is now ready to use. We can compile and run our program myfile.c now using our dynamic library dynLibrary.so.

$ gcc -Wall -Werror -o myfile.c dynLibrary.so

Advantages of a dynamic library are:

  • Dynamic libraries load only a single copy of the library file in memory when you run a program. As a result a lot of memory is saved when you start running multiple programs using that library.
  • If any update is made to the functions inside a dynamic library, your programs do not need to be recompiled ,whereas when using a static library you had to recompile. This type of runtime dynamic linking allows for easier library maintenance.

Disadvantages are:

  • A dynamic library takes more time to load and get the program running because the linking occurs at runtime instead of during compilation. Because of this many softwares still use static libraries for speed.

I hope this article has given you some more insight into both static and dynamic libraries, and perhaps this new information will help you when deciding which of the two libraries to use in your next project!

--

--