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 --
2. DRAWBACKS
-- Dynamic Library --
Recommended by LinkedIn
2. DRAWBACKS
How to create a Dynamic library?
To create a dynamic library you have to follow these simple steps:
To create a dynamic library in Linux, simply type the following command:
and hit return. This command essentially generates one object file .o for each source file .c:
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:
(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
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: