The differences between Static and Dynamic (Shared) libraries ?

Ahmed Omar MILADI
4 min readDec 17, 2019
  • Why using libraries in general ? In programming, a library is an assortment of pre-compiled pieces of code that can be reused in a program. Libraries simplify life for programmers, in that they provide reusable functions, routines, classes, data structures and so on (written by a another programmer), which they can use in their programs.

For instance, if you are building an application that needs to perform math operations, you don’t have to create a new math function for that, you can simply use existing functions in libraries for that programming language.

Examples of libraries in Linux include libc (the standard C library) or glibc (GNU version of the standard C library), libcurl (multiprotocol file transfer library), libcrypt (library used for encryption, hashing, and encoding in C) and many more.

  • How do they work ?

When you want to use a program you have written in C, you need to compile it into a language that your computer will understand, that is, the compiler will translate your source code into object code. The compiler, really combines four processes which take place in order and are made effective through: the preprocessor, the compiler proper, the assembler and the linker.

It is at the linking phase that the links are made between your program and any necessary libraries. There are two important types of libraries, Static libraries, and Dynamic (or Shared) libraries.

When using a Static library, code from the library will be written into your program. However when using Dynamic libraries, rather than write the actual code into your program from the library, the only the memory address is written into your program so the necessary code can be retrieved from the Dynamic library at run time.

How to create and use a Static libraires or Dynamic librairies ?

Static libraries : is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries (libraries which are statically linked) are, .a files in Linux and .lib files in Windows.

  1. Compile library files : gcc -c file.c -o file.o
  2. Create static library: ar rcs file.a file.o
  3. Run program: ./file

Dynamic, or shared libraries : Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory. Examples of Dynamic libraries (libraries which are linked at run-time) are, .so in Linux and .dll in Windows.

  1. Compile library files : gcc *.c -c -fpic
  2. Create a shared library : gcc *0 -shared -o liball.so
  3. Export: export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

Differences between Static and Dynamic libraries: Advantages and Disadvantages:

When considering the advantages and disadvantages of Static and Dynamic libraries, we may want to consider size, speed, and updates. Since the actual code from Static libraries is written into your program, when it comes time to run the program, the code is already there, and so, your program may run slightly faster than programs that must, at run time, go and retrieve code at a given memory address. However, your object code files will be larger when using Static libraries, and if you have many of them, the added space taken up on the disk may be an issue to consider. Another issue with Static libraries concerns updates. If you need to update material in a Static library, the only way to update all the programs that already have the corresponding code written in them, is to re-compile those files. If they are few in number, this may not be an issue, but if there are many, Dynamic libraries may be preferable. When it comes to compatibility, Static libraries offer an advantage: since the source code contains the code from the library, there are not compatibility issues that arise from updates to a Dynamic library. So now, when we consider Dynamic libraries, we can see their advantages and disadvantages. A Dynamic library, while it may make your program run a little slower at run time, will save space on your disk because you will only be writing a memory address into your object code instead of the much longer code itself. And when you need to make updates, instead of recompiling all your files, you simply need to update the code in your Dynamic library. The programs that use it store only the memory address, so one update will suffice for all your programs

--

--