What’s the difference between Dynamic libraries and Static libraries!

Yosri Bouabid
2 min readMay 4, 2020

First of all what’s a Dynamic library:

Also called shared library is a collection of functions compiled and stored in an executable with purpose of being linked by other programs at run-time, which minimizes overall program size and facilitates improved application performance for reduced memory consumption.

It is never part of an executable file or application, also small in size because there is only one copy of dynamic library that is kept in memory at the time of execution only otherwise its location is remote.

Dynamic library are faster because shared library code is already in the memory.

On other hand in case of Shared library has compatibility issue as target program will not work if library gets removed from the system .

What about the static library:

It is a compiled object file containing all symbols required by the main program to operate.

they aren’t loaded by the compiler at run-time, also they are large in size because external programs are built in the executable file.

Recompilation is required if any changes were applied to external files.

It take to long to execute, because loading into the memory happens every time while executing.

How to create this libraries:

1- static library:

first create the object files (file.o) from the c files with the command:

gcc -c *.c

And then transform all the .o files to the library:

ar rcs lib.a *.o

ar : is the command to create a static library

lib.a: the name of the library

  • .o: all the files with the format (.o) .

2- Dynamic library:

First step as always is to create the object files (file.o) from the c files with the command:

gcc -c *.c

And then:

gcc -shared -o liball.so *.o

So that all the .o files created will be stored in the liball.so library.

--

--