Libraries : Static Libraries vs. Dynamic Libraries

Onsjannet
4 min readSep 7, 2020

To make life easier we use Functions, functions are blocks of code that can be re-used to gain time, it removes the need to rewrite code over and over again and this is where libraries comes in hand it makes functions reusable in multiples programs.

So How Do libraries Work?

  • Static Library :
    we 1st need to compile the source files of the functions and depends on the platform the extension can be .o or .obj and anything else. Static libraries are a sort of a collection of object files. when we try to generate an executable the linker tries to resolve the referenced symbols by linking them together and by locating in which object they are defined. Static libraries might contain index of defined symbols to facilitate this.
  • Dynamic Library :
    Dynamic libraries provides code that could be loaded anywhere in the memory. And once loaded, the programmer would be able to use the code by any number of programs . And since code is kept in a form of a shared library the memory used by the program could be low.

How to create Static Libraries ?

  • 1st step is to regroup all relevant files into one directory including the hedear (.h) file, making sure it contains the #ifdef HEADERFILE_H, the #define HEADERFILE_H and ends with #endif.
  • 2nd step is to compile all the (.c) files and Turn them into an object file (.o) using the command “ $ gcc -c *.c
  • 3rd, Once the (.c) files compiled to their respective oject files we creat our library using the GNU “ar”. In our case this would be the command to use “ $ ar -rc libholberton.a *.o”
  • 4th passing to the next step; the “ranlib” step to index our library. The index lists each symbol defined by a member of an archive that is a relocatable object file. “ $ ranlib libholberton.a”
  • 5th we can check the content of our library using the “ar” option “-t”. “ar -t libholberton.a”, we can always check the symbols in our libraries as well using the command “nm” in our case “ nm libholberton.a”

How to create Dynamic Libraries ?

  • 1st we need to generate object files .o by using gcc *.c -c -fPIC, -fPIC is to make sure that the code is position-independent. So it doesn’t matter where the code is loaded into the memory. The -c options just to make sure that the.o files aren’t linked yet.
  • 2nd we need to type gcc *.o -shared -o liball.so and by inputting The wildcard * we tell the compiler to compile all the .o files into a dynamic library and that is specified by the -shared flag. Naming Dynamic Libraries it has to always start with lib and ends with .so .
  • 3rd we export using export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH so the program knows where to look for library files, we must add that location to the environmental variable LD_LIBRARY_PATH.

What are the differences between static and dynamic libraries and what are both the advantages and disadvantages of the both of them?

the advantages and disadvantages of both static libraries and dynamic libraries comes as differences between both of them so the advantages of static libraries are disadvantages to dynamic libraries and vise versa the following explanation would make it easier :

Linking time :
- Static Libraries :
when the program is being placed in the memory, Linking is the last step of the compilation process.
- Shared or Dynamic Libraries : When the executable files and the libraries are added to the memory. the shared libraries are added during the linking process

Means:
- Static Libraries :
by linkers
- Shared or Dynamic Libraries : by operating System

Time:
- Static Libraries : takes longer than the dynamic libraries and that’s a disadvantage compared to the dynamic libraries
- Shared or Dynamic Libraries : is quicker than the static libraries and that’s an advantage compared to the static libraries.

Compatibility:
- Static Libraries : no compatibility issues and that’s an advantage compared to the dynamic libraries
- Shared or Dynamic Libraries : they won’t work if the library is removed from the system. and that’s a disadvantage compared to the static library.

External file changes:
- Static Libraries : recompiled after every changes applied to external files and that’s a disadvantage compared to the dynamic libraries.
- Shared or Dynamic Libraries : no need to recompile the executable and that’s an advantage compared to the static libraries.

And these are the differences between static libraries and dynamic libraries and the reason why static libraries lost their fame and been replaced by Dynamic or shared Libraries.

--

--