What is the difference between static and dynamic libraries?

This is a brief summary of static and dynamic libraries, how they work, how to create them, and how to use them in your projects.

Let’s start defining, what is a library?

It is a compilation of the prototypes of functions that we use in our programs, the functions in the C programming language, are declared and stored in a library, to be used at the moment that we need them, This saves a lot of time at the time of executing a program, accelerating the compilation process.

Libraries in the C programming language can be of two types: shared or dynamic and static.

How do these libraries work?

Static libraries, unlike dynamic libraries, gather object files into one, while dynamic libraries exist as separate files outside the executable, which means that static libraries add those files when linking, before it becomes an executable binary and dynamic libraries also have object files but they are not added during compilation, they are kept separate from the executable binary, they are added to memory so that during execution those object files we need are kept available (linking process, final step of compilation).

Static libraries do not change as long as we do not edit the functions contained in them, while dynamic libraries add functions at the time of compilation during execution, so it is not necessary to compile the program again and again with dynamic libraries.

How to create and use libraries?

To create static or dynamic libraries, we have to start by compiling all our “.c” files in this way:

gcc -c * .c

For the dynamic libraries, we add the flag: “-fPIC”:

gcc -c -fpic * .c

With the above instructions, we will create the object files for both static and dynamic libraries. The “-fPIC” flag that we add to the compilation for the creation of dynamic libraries, tells the compiler that the object code is position independent. The next step is the creation of the library.

To create a dynamic library we use the following command:

gcc -shared -o name_library.so file_object.o

The “-shared” flag instructs the compiler to convert the “.o” files into a dynamic library so that our object file can be linked with other objects to form an executable file.

The “-o” flag places the output in a file.

If all went well, then we should have created a file with a “.so” extension, which stands for the shared object.

Some useful commands:

nm - List all objects in al library

nm file...

ldd - prints the shared objects address

ldd [option]... file...

ldconfig - Create update and remove symbolic list for the current shared libraries in /etc/ld.so.conf

ldconfig    [-DvnNX]    [-f conf]   [-C cache]   [-r root]

How do we use it?

One difference between dynamic libraries and static libraries is that the former refers to the library that is placed in your program and the latter, on the contrary, copy the entire library.

Next, we will link the functions in the code once the dynamic library has been created:

gcc -Wall -pedantic -Werror -Wextra -L. main.c  l’name_library‘ -o

The “-L” flag tells the compiler to take the function it needs from the “.c” file, then place the library name with the “-l” flag without the “.so” extension, when linking it is not important to point out these endings because the compiler assumes it automatically.

For now we will focus on the use of dynamic libraries, if you want to know how to create and use static libraries you can read my other post, “Static libraries in C”.

What is $ LD_LIBRARY_PATH

Before continuing, we have to verify that the library is installed in its standard location (/ lib: usr / lib: usr / local / lib). To do this, we use the environment variable LD_LIBRARY_PATH. In this variable we first look in a group of directories where we could find the shared libraries:

/lib/usr/lib/usr/local/lib

Then we add the library path with the command:

export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH

To verify that the above process has been performed well, you can use the “nm” command (this command lists the symbols of the object files) along with the name of your library, you will see a list of the functions included within the library.

At this point: What are the advantages and disadvantages of each of them?

Advantages of static libraries:
- The libraries are pre-loaded in an executable file.
- They are not required at runtime and are not included at runtime.
- Longer compilation time and speed than linking to individual files and increasing the size of the binary code.
- Since the object code is included in the executable, multiple function calls are handled.

Advantages of dynamic libraries:
-Programs using them do not need to be compiled in case something is edited in the code.
-At runtime, a copy of the files is created so that they are outside the executable file.
-Memory saving when multiple library files are executed.
-It does not need to be compiled again and again in case of having some edition after this process.

Disadvantages of static libraries:
- Every time you want to add more functions to the library, you have to compile it.
- When you have a copy of the executable, it can become too large a file.

Disadvantages of dynamic libraries:
- Long load times and performance in terms of execution and linking.
- Compatibility issues, if you change the library, the program that uses that library may not work and you have to make adjustments.

Now that you know all this, put it into practice in your projects.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store