Creating Dynamic libraries (Linux, C) and Usage
Youssef Jellouli
3x Certified Salesforce and Mulesoft Developer | Graduate Software Engineer
Creating Dynamically Linked Libraries has never been easier.
With the GCC (GNU Compiler Collection) compiler it is very easy to Create a Dynamically Linked Library.
First of all we need to make sure we have some functions ready for us to compile for this article's sake, ill be using a simple implementation of the strlen() and putchar() functions in C.
strlen() Function.
putchar() Function.
Now that we have our functions and included them in the "main.h" header file
in the same directory as the functions we will use the command
gcc -fPIC -c *.c
This will turn all of the current source files ending in (.c) to generate our new object files (.o) that are position independent (more on this later)
Then, we use this second command to create our dynamically linked library called libdynamic.so
gcc -shared -o libdynamic.so *.o
This will take all the shared object files (.o) from the first command and *squish* them into a dynamically linked library.
领英推è
Great job! We created a Dynamically linked library.
Okay.. well, why did we create a Dynamically linked library instead of a Statically linked library.
Statically linking a library is very speedy..... for little projects, it basically will take the
Includes and our compiler (linking stage specifically) will unwrap these headers and turn them into source code and includes it in our binary, this comes with a disadvantage in big projects due to the binary size, imagine if we were to have a project with 200 functions? all imported from different libraries... this will increase the size of our binary executable and that's not very good also imagine if a developer were to update their library, the executable itself since it has static code it will not update.
On the other hand, using Dynamically Linked Libraries fixes these issues! On execution, these libraries are loaded into memory, and in our executable instead of having all of the source code included, it will have the ADDRESS of the function from the library in memory
So instead of having a bloated file size, we just have this tiny little address in our hand that points to the functions in memory, and since we created the Library with the -fPIC option in GCC, the library is position independent so it could be anywhere in memory.
How do I use the Dynamically Linked Library if it isn't in the executable source code?
In the compiling process we will use the command:
gcc -L main.c -ldynamic -o exec
This tells our program that we are using the library called dynamic in compilation and outputs the binary called exec, but our binary will not execute unless we do the following:
In Linux, we have Environment variables, Our executable will look for the dynamically linked library in the $LD_LIBRARY_PATH variable so we have to add our file location to the path, this could be any location on your device.
The variable could be set using EXPORT in your Terminal for example we can use
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
This adds the . (current folder) location to our Environment variable LD_LIBRARY_PATH. and then our executable will work fine.