C libraries, static and dynamic.

Nour Ouhichi
3 min readMay 4, 2020

How to use a library ? What is its use ? How does it work (Linux only)? And what is the deference between a static and a dynamic library? In order to get to these points you have to make a clear idea about the concept of a library.What is a library?

What is a C library?

A library is a collection of header files which other programs could reach. It contains one or more object files that is normally the output of a compiler or an assembler.These files are turned later into a library or an archive

How is it useful?

We use a library to get pre-defined output instead of writing the same function every-time or writing our same code. It saves time by making sure the code contains no bug

How to make a library and use it ?

First thing first , you have to build your C program which contains the functions you want to make a library of.Second, include your functions prototypes in a header (a .h extension file) then compile your files into object files.You may use this command.

gcc -fPIC -c *.c

0-make a static library:

  • You may use the following command:
ar rcs libmylib.a objfile1.o objfile2.o objfile3.o
  • This will create a static library called libname.a. Rename the “mylib” portion of the library to whatever you want.Now to use the library make sure you link them and specify where to find the library by the command -L :
gcc -o foo foo.o -L. -lmylib
  • The -L. piece tells gcc to look in the current directory in addition to the other library directories for finding libmylib.a.

1-make a dynamic library:

  • After compiling your program into object files by the following command
gcc -fPIC -c objfile1.c
  • we may use now the principal program with the library we created by this command:
gcc -o foo foo.o -L. -lmylib

The problem now is that your program is not used to working with dynamic libraries, in this order you have to run the following code:

echo $LD_LIBRARY_PATH

What is the difference between static libraries and dynamic libraries what are the advantages and drawbacks of each of them?

00-Changes:

  • When modified , executable files for static libraries must be recompiled for the archive to state the changes whereas in shared libraries, no need to recompile the executable.
  • 01-size: Static libraries are much bigger in size, because external programs are built in the executable file while Dynamic libraries are much smaller, because there is only one copy of dynamic library that is kept in memory.
  • 02-Time: Static libraries take longer to execute, because loading into the memory happens every time while executing.A dynamic one is faster because shared library code is already in the memory.
  • 03-Compatibilty: a static library never has compatibility issue, since all code is in one executable module while with a dynamic one programs are dependent on having a compatible library. Dependent program will not work if library gets removed from the system .
  • 04-Linking time: the linking for the static library happens as the last step of the compilation while it happens at the linking stage with the dynamic library.
  • 05-Means: static libraries are performed by linkers , dynamic ones are performed by the operating system

Libraries are here to save us time and energy a dynamic library is known to be more efficient .

--

--

Nour Ouhichi

Software engineering student at Holberton school with the passion of sharing my knowledge. Simplified language articles about different programming languages.