The differences between static and dynamic libraries

Bouzouitina Hamdi
5 min readMay 4, 2020

--

Libraries make life easier.

Why using libraries in general!

A library is a file that can be used as a single entity in the linking phase when compiling a file. they are collections of object files that are linked together when a file gets compiled into an executable file.

The benefit of using a library is that the functions and other symbols loaded into it are indexed and the program need only reference a single single archived object file that has ordered the entities together.

How do they work!

When we compile a source file we get an object file. Depending on our platform its extension .o . A library is basically a collection of object files, kind of like a .zip file but probably not compressed. The linker, when trying to generate an executable tries to resolve the referenced symbols, i.e. locate in which object file (be it in a library or otherwise) they are defined and links them together. So, a static library may also contain an index of defined symbols in order to facilitate this.

How to create them!

Static library :

1-The first step is determining which functions we would like to include in our library and preparing them for the archiver.

2-Once this command is entered, the corresponding .o files will be present in the directory.

3-Now we have a list of object files from our C files that are going to be used in our static library. Our next step is to create the static library using the ar program with all of our object files or .o files.

4-Archive all of the object (.o) files into one static library (.a) file. Use the command option -r to ensure that if the library (.a) file already exists, it will be replaced. The command option -c should be used so that if the file doesn’t exist, it will be created.

Dynamic Libraries:

To create a dynamic library, write the following command:

With the “*.c” it takes all of the C source files in the current directory and makes a shared library called “liball.so”. The “-f PIC” flag allows the following code to be referenced at any virtual address at runtime. It stands for Position Independent Code.The library does not hold data at fixed addresses because its location in memory will change between programs. Object files get compiled by using the “-shared”.

How to use them!

Static library :

Now, instead of having to include all relevant file names in the compilation command, only the library (.a) file need be referenced.

Dynamic Libraries:

Using a shared library is done in this steps:

  1. Compile Time here we need to tell the linker to scan the shared library while building the executable program, so it will be convinced that no symbols are missing. It will not really take the object files from the shared library and insert them into the program.
  2. Run Timewhen we run the program, we need to tell the system’s dynamic loader (the process in charge of automatically loading and linking shared libraries into the running process) where to find our shared library.

What are the differences between static and dynamic libraries!

Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files. All the code relating to the library is in this file, and it is referenced by programs using it at run-time. A program using a shared library only makes reference to the code that it uses in the shared library.

Static libraries are .a (or in Windows .lib) files. All the code relating to the library is in this file, and it is directly linked into the program at compile time. A program using a static library takes copies of the code that it uses from the static library and makes it part of the program. [Windows also has .lib files which are used to reference .dll files, but they act the same way as the first one

What are the advantages and drawbacks of each of them!

  • Shared libraries reduce the amount of code that is duplicated in each program that makes use of the library, keeping the binaries small. It also allows you to replace the shared object with one that is functionally equivalent, but may have added performance benefits without needing to recompile the program that makes use of it. Shared libraries will, however have a small additional cost for the execution of the functions as well as a run-time loading cost as all the symbols in the library need to be connected to the things they use. Additionally, shared libraries can be loaded into an application at run-time, which is the general mechanism for implementing binary plug-in systems.
  • Static libraries increase the overall size of the binary, but it means that you don’t need to carry along a copy of the library that is being used. As the code is connected at compile time there are not any additional run-time loading costs. The code is simply there.

--

--

No responses yet