C dynamic libraries

Jacer Dabbabi
3 min readMay 4, 2020

What are libraries ?

Libraries are a collection of programs and packages that are made available for common use within some environment; individual items need not be related. A typical library might contain compilers, utility programs, packages for mathematical operations, etc. Usually it is only necessary to reference the library program to cause it to be automatically incorporated in a user’s program.

There are two types of libraries used in C:

  • static
  • dynamic (or shared)

Here we are going to talk about dynamic libraries , for more information about static libraries , please visit : My post .

Why use libraries ?

The main reason is that libraries make the compilation easier .

How do they work ?

Dynamic (shared) libraries are libraries that are loaded by programs when they start. When a dynamic library is installed properly, all programs that start afterwards automatically use the new dynamic library.

Dynamic or shared libraries can further be categorized into:

  • Dynamically linked libraries — here a program is linked with the shared library and the kernel loads the library (in case it’s not in memory) upon execution.
  • Dynamically loaded libraries — the program takes full control by calling functions with the library.

How to create them ?

First we need to compile our C files into object code. We can do that using:

gcc -fPIC -c *.o

-c : for compiling source files without linking them

-fPIC : a direction to the compiler to output position independent code that is required by shared libraries

Next, we need to create our dynamic library and add files to it. We can do this with the command:

gcc -shared -o libholberton.so *.o

Because a program needs to know where to look for library files, we must add that location to the environmental variable LD_LIBRARY_PATH.

export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

How to use them ?

To use out shared library we need to link it to our program when compiling it .

To compile our program -let’s name it program.c- file and link it with shared library we need to run this code:

gcc program.c -lholberton

What are the differences between static and dynamic libraries ?

  • A program using a shared library only makes reference to the code that it uses in the shared library but the static library makes the code of the functions (listed in the library) a part of the program.
  • A dynamic library is linked to the program but for a static library the linking happens when the client executable using the static library is built.
  • A dynamic library is needed after the executable is built but astatic library is only needed during building the executable (during link stage).

What are the advantages and drawbacks of libraries ?

Although dynamic libraries allocate less space and less memory , they are always needed after building the execution file . One other thing, Only the functionality in the Dynamic library needs to be recompiled but we might run into issues when a version of the dynamic library used by our program conflicts with an older/newer version of the library present on the operating system.

Static libraries are simple , they are distributed by default with the application/ executable itself ; Because they are only needed during building the executable ,it is not needed to run the executable because the library code is embedded inside the application but that will make it take more space . last , Since all the library functionality is linked into the application, it does not amtter if other applications on the system is using a different version of the static library.

enjoy :D

--

--