The differences between static and dynamic libraries.

Mhamed Azouzi
3 min readMay 4, 2020

Why using libraries in general?

The C standard library provides macros, type definitions and functions for tasks such as string handling, mathematical computations, input/output processing, memory management, and several other operating system services.

How do they work?

After writing our C code and as we reach the linking process, which is the last step of the compiling process :

It links our code with that library function code ( Among system functions …). The linker as for the Static libraries :

makes a copy of all the library functions to the executable file. The library will remain the same even there was a change in the original library unless the program is recompiled.

as for the Dynamic libraries makes a copy of the address in memory of all used libraries to the executable file. Every change in the content of the library will affect the program without even recompiling it.

How to create them? (Linux only)

Static libraries:

1: Compile your .c files to object code files.

$ gcc -c *.c

2: Create a Static Library from those object files “.o”.

$ ar -rc libStatic.a *.o

using the c and r arguments of the ar command to create the library and replacing older files if need be.

PS: In case we want to check our library contents:

$ ar -t libStatic.a

3: Create an index for the library.

$ ranlib libStatic.a

4: Use the static library in the compilation and linking process when creating an executable file.

$ gcc myprog -L. -lStatic -o exe

Dynamic libraries:

1: Compile your .c files to object code files using -fPIC arguments.

$ gcc -fPIC -c *.c

2: Create a Dynamic Library from those object files “.o” using -shared argument.

$ gcc -shared -o libDynamic.so *.o

3: Adding the Dynamic library path to the library path environment variable so It can be found in the linking process.

$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

4: Use the Dynamic library in the compilation (linking) process when creating an executable file.

$ gcc -L. -lDynamic -o exe

The difference between them? And what are the advantages and disadvantages:

Static libraries:

1- The executable file takes a copy of the used library, as a result, the size is bigger.

2- if any changes in the libraries needed to be updated in that file it should be recompiled.

3- It takes less time to execute since the libraries needed are in the same file and no calls needed.

4- Since everything the program needs, is compiled within, it is compatible and transportable.

Advantages:

  • Compatibility
  • Only the executable file is needed
  • faster runtime

Disadvantages:

  • With every change compiling is needed
  • Larger file size
  • Every Program using the same library need to load its copy of the library in the memory

Dynamic libraries:

1- The executable file takes a copy of the address the used library in memory, as a result, the size is smaller.

2- No need to recompile the file if any changes accrued in the libraries

3- Executing the program is significantly slower the library is loaded from memory since it’s not in the file.

4- The program is executed only when the library is found.

Advantages:

  • Lower file size
  • No need for compiling when updating library contents
  • faster to compile especially if the library is already in memory
  • it needs to be loaded only one time and it can be used by multiple programs at the same time

Disadvantages:

  • No Compatibility
  • Dependent on more than just the executable file
  • Slower run time

--

--