Dynamic Libraries in C

Dynamic Libraries in C

In my article located below, I gave a detailed explanation of how to create and use dynamic libraries. If you need a refresher, please check it out! In this article, I will focus on explaining how exactly to create and use dynamic libraries in the C programming language.

Why use libraries in C?

-- What is a function in C? --

First of all, it is necessary to define what is a function, this is a named section of a program that does a specific task. Functions can be used throughout the program to save time by employing aforementioned function to perform a task as opposed to rewriting that specific block of code when you want to do the task again later in the program.

-- What is a library in C? --

It is a compilation of the prototypes of functions that we use in our programs, the functions in programming language C, are declared and stored in a library.

Libraries in C are not unlike public libraries in cities, towns, or neighborhoods. A public library provides access to a multitude of information in various media forms to the public for access and use. Functions in a C library can be used and accessed by programmers to create several different programs.

As a programmer, you may find yourself using the same function or functions repeatedly. In this case, it is best to put this function or functions in a library to speed up the compilation of the program. C libraries store files in object code; during the linking phase of the compilation process ( Compilation Process) files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.

differences between dynamic and static library

-- Static Library --

  • They are part of the build environment.
  • Object files are added to the executable file.
  • They hace the .a extension.

  1. ADVANTAGES

  • Library previously loaded in an executable file.
  •  Not required at runtime or included when running.
  • Faster compiling time and speed than linking to individual files and increasing binary code size.
  •  As the object code is included in the executable, multiple function calls are handled.

2. DRAWBACKS

  •  Each time you want to add more functions to the library, it must be compiled.
  • When you have a copy of the executable, it may become too large a file.

-- Dynamic Library --

  • They are of the run-time environment.
  • Address of the object files are added to the execucatble file.
  • They have the .so extension.

  1. ADVANTAGES

  • The programs that use them don’t need to be compiled in case something in the code is edited.
  • At runtime, a copy of the files is created so they are outside the executable file.
  • Memory saving when running multiple library files.
  • It doesn’t need to be compiled over and over again in case you have any editions after this process.

2. DRAWBACKS

  • Long loading times and performance in terms of execution and binding
  • Compatibility problems, if you change the library, the program that uses that library may not work and adjustments may need to be made.


How to create a Dynamic library?

No hay texto alternativo para esta imagen

To create a dynamic library you have to follow these simple steps:

To create a dynamic library in Linux, simply type the following command: 

No hay texto alternativo para esta imagen

and hit return. This command essentially generates one object file .o for each source file .c:

No hay texto alternativo para esta imagen

The -fPIC flag ensures that the code is position-independent. This means it wouldn’t matter where the computer loads the code into memory. Some operating systems and processors need to build libraries from position-independent code so that they can decide at runtime where they want to load it into memory. The -c options just ensures that each .o file isn’t linked yet.

Next, type in the following command: 

No hay texto alternativo para esta imagen

(substitute your desired library name with all) and hit return. The wildcard * tells the compiler to compile all the .o files into a dynamic library which is specified by the -shared flag. The naming convention for dynamic libraries is such that each shared library name must start with lib and end with .so

No hay texto alternativo para esta imagen

Other than that though, let your imagination run free when considering names for your dynamic libraries.

Finally, we’ll need to export the path for libraries so that programs know where to look for them by executing the following command:

No hay texto alternativo para esta imagen


To view or add a comment, sign in

More articles by Angel Piva

  • Ahorrapp - Holberton School

    Do you like saving money? Do you think this habit is useful to improve economy? Well, Ahorrapp is now available!…

    2 Comments
  • What happens when you type google.com in your browser and press Enter

    Everyone has done a search for something in their browser at some point, but not many people know what´s happening in…

  • What happens when you type `ls -l *.c` in the shell?

    This blog was thought for those people who don´t know about what is a shell and what does it does, so, if you want to…

  • C Static Libraries

    ¿Why use libraries? Libraries in C are not unlike public libraries in cities, towns, or neighborhoods. A public library…

Others also viewed

Explore content categories