‘C’ Dynamic Library
Why using libraries in general?
Developed programs are likely to expand larger and larger as more functions are added to solve new problems. This leads to the extension during compilation and linking time. To address this problems, compilers supply users with ‘libraries’.
A library is a single file containing several object files which is used in the linking phrase of the compilation process. A library allows programmers to reuse their previously-coded functions, routines, classes, data structures and so on to make their program more efficient.
Unix systems allow us to create two kinds of libraries: Static libraries and Dynamic (Shared) libraries.
Static libraries are collections of object files that are linked with the program during the linking phase of compilation, but are irrelevant during runtime of the program. To further understand about Static libraries and how to create and use it, please visit my previous post.
Dynamic (Shared) libraries are linked to the program in two stages:
- During compile time, the linker verifies that all the symbols ( functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries.
- However, the object files from the dynamic library are not inserted into the executable file. Instead, when starting the program, a dynamic loader checks out which shared libraries were linked, loads them to memory, and attaches them to the copy of the program in memory.
This blog will discuss how to create and use Dynamic libraries, and explore the differences between Static libraries and Dynamic libraries.
How to create a Dynamic library?
Step 1: Create an executable file that will contain the dynamic library
touch libraryName.so
- touch: a Shell command in Linux to create a new file.
- libraryName.so: the wanted name for the new file. Must have prefix ‘lib’
chmod u+x libraryName.so
- chmod: a Shell command in Linux to change file system modes of file.
- ‘u’ and ‘x’: The execute permission.
- libraryName.so: the file that needed execute permission.
Step 2: Compile .c source file of functions inside the dynamic library.
gcc -c -fpic *.c
- gcc: GNU compilation command.
- ‘c’ flag: Stop the compilation before the Linking phase.
- ‘fpic’ flag: Generate Position Independent Code for shared libraries.
- *.c: All source files in the current directory.
Step 3: Compile the object codes to create a shared library.
gcc -shared *.o -o libraryName.so
- ‘shared’ flag: Generate shared object file for shared library.
- ‘o’ flag: Modify the file name.
- *.o: All object codes in the current directory.
- libraryName.so: The name wanted for the library.
During compilation, the compiler identifies a library file by looking for files beginning with ‘lib’ and ending with a library extension (.so for dynamic and .a for static) accordingly.
How to use a dynamic library?
Step 1: Add the file location to the environmental variable LD_LIBRARY_PATH to help the program to find the path to look for libraries
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
- export: a command to make use of variables and functions that are to be passed on further to all child processes.
Step 2: Compile the program with the shared library
gcc -L -l(lib)Name file.c
- ‘L’ flag: look in the current directory for the library file
- ‘l’ flag: link with library file
- Name: the name of the wanted library
- file.c: File needed the library to be linked into.
For example, gcc -L -lholberton main.c command will tell the compiler to look for a library named libholberton.so and link it to the main.c file.
Differences between static and dynamic libraries?
Reference: