Static and dynamic libraries.

Duvan Jaramillo
3 min readMay 5, 2020

--

What is a dynamic library

A dynamic library are several functions compiled, this allows an user to link functions to their object file’s during either run-time or compile time.

The static library is composed of several object files, while the dynamic or shared library is a collection of functions compiled and stored in an executable for the purpose of being linked by other programs during execution.
Dynamic libraries provide a means of using code that can be loaded anywhere in memory. Once loaded, library code can be used by any number of programs. In this way, the size of programs using the dynamic library and the memory footprint can be kept low since much code remains common in the form of a shared library.

Why use static libraries?
Basically because they are faster in execution, since the functions are inside the executable and we don’t have to look for them. A C library is a set of several functions that we normally use to write our programs, for example the standard library “stdio.h” contains various functions such as printf, puts, putchar, etc. We can also create our own libraries for C, the first thing we have to do is create our own functions, once we have our functions created and our ‘.h’ file (header with all the function prototypes), we can start creating our own static library.

How to create dynamic Library Creation (Linux only)

gcc *.c -c -fpic’
The .c source files need to be prepared for use in a dynamic library. Since we need to apply this step after the compile process has generated the object code, the compiler must be told to halt and return one object file (.o) for each source file. This is done by using the -c flag.

‘gcc *0 -shared -o liball.so’
The object files are now ready to be compiled into a dynamic library. This is done by compiling all of the .o files using by using the -shared flag.

What are the differences between static and dynamic libraries.

Advantages and drawbacks of each of them.

Static libraries are much bigger in size, because external programs are built in the executable file.

Dynamic libraries are much smaller, because there is only one copy of dynamic library that is kept in memory.

Static libraries takes longer to execute, because loading into the memory happens every time while executing.

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

Static libraries. never has compatibility issue, since all code is in one executable module.

Dynamic libraries. programs are dependent on having a compatible library. Dependent program will not work if library gets removed from the system .

Resources.

https://www.geeksforgeeks.org/difference-between-static-and-shared-libraries/

--

--

No responses yet