Creating and using Dynamic Libraries in “C”
Not long ago, I wrote a detailed explanation of how to create and use static libraries, just in case you need a refresher, please check it in the link below:
How to Create and Use a C Static Library
A static library is always available when invoked by the program, In this article, I will explain what static library…
Otherwise, I will start explaining how to create and use dynamic libraries.
“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.” -Bill Gates
Before we go ahead and jump into the definition of dynamic libraries, I need to make clear the definition of a function, a function in C programming is an algorithm that does a specific task, and can be reused as many time as need it avoiding to rewrite many times hence saving time while coding. Although static libraries can be used by multiple programs, once the program is compiled is not possible to change the different pieces of object code in an executable file as all the code is within a single file that was statically linked when the program was compiled.
Unlike static libraries, dynamic libraries consist of a separate file containing separate pieces of object code. These files are linked together in a single piece of object code, hence dynamic libraries contain extra information that the operating system will need to link the library to other programs. How to Create a Dynamic Library (Linux)
How to Create a Dynamic Library (Linux)
To create a dynamic library in Linux, type the following command:
gcc *.c -c -fPIC 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 ensure that each
.o file isn’t linked yet.
Next, type in the following command:
gcc *.o -shared -o libcalc.so (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:
Let’s work with a simple example:
We have a program written in C that contains the basic functions of a calculator, a header with all the prototypes of the program functions, and an entry point knows as the main function which calls the calculator program.
* calculator - Functions calculator
* @a: first number
* @b: second number
* Return: the result of each equation
int add(int a, int b)
return (a + b);
int sub(int a, int b)
return (a - b);
int mul(int a, int b)
return (a * b);
int div(int a, int b)
return (a / b);
int mod(int a, int b)
return (a % b);}
The name of the program above is “func_ops.c”, after we name or function we now go ahead and the object file, using the following commands:
vagrant@vagrant-ubuntu-trusty-64:~/dynamic_libraries$ gcc -fPIC -c func_ops.c
vagrant@vagrant-ubuntu-trusty-64:~/dynamic_libraries$ ls func_ops.c. func_ops.o
After our compiler does his job an object file is created, which allow us to create now the dynamic library with the following commands:
vagrant@vagrant-ubuntu-trusty-64:~/dynamic_libraries$ gcc -shared -o libcalc.so
func_ops.c func_ops.o libcalc.so
How to Use a Dynamic Library (Linux)
To use a dynamic library, it is needed to export the LD_LIBRARY_PATH which tells the dynamic link loader (ld. so — this little program that starts all your applications) where to search for the dynamic shared libraries an application was linked against.
Finally, to export the path for libraries so that programs know where to look for them by executing the following command:
vagrant@vagrant-ubuntu-trusty-64:~/dynamic_libraries$ export LD_LIBRARY_PATH=$PWD/libcalc.so:
That is all! And now that you know how to create and use dynamic libraries I hope you have fun coding!
If you have any questions, comments or suggestions, feel free to contact me on Twitter @MrTechi_