What is a Dynamic Library? How does it work? And how does it compare to my beloved Static Libraries?
Why?
When creating our code perhaps our most important piece of code is our functions, using functions allow use to execute with out needing to write multiple time wasting lines of code. With multiple functions we can execute our programs. But how can we properly organize all the functions we are working with?
Simple we use libraries
How do these libraries work? And how are they created?
Programmers use libraries to organize the functions used with programs. There are two types of libraries: Static and Dynamic Libraries.
For information on Static Libraries and how to create one, the following link contains information on such:
In order to create a dynamic library we need to first have a list of source files (.c) with functions in said files.
From this collection of .c files we will generate object files (.o), to do this we run a command with the gcc compiler.-gcc c -fPIC *.c
gcc c -fPIC *.c
The -fPIC is used to make the code "position independent", which in simpler terms means that it will not matte3r where the computer loads the code into memory.
After we have created all of these object files we need to put them all together into one library, once again we will do this with a gcc command.
Recommended by LinkedIn
gcc -shared -o liball.so *.o
Now we have created a library named "liball.so", notice the -shared flag which specifies this will be a dynamic library. There is a specific naming rule for libraries in which their name must start with "lib" and end with ".so".
Only step after creating the library is to simply export the path for libraries so it can be accessed when executing programs. To do this we use the following:
export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
How do we use Dynamic Libraries?
Once we have our library created and exported it's time to use that library, to do this will see with the following example.
gcc =L test.c -lall -o test
As we can see with the above code we are compiling our test file "test.c" and after that we use the "-l" flag in combination with "all" where this tells the compiler to search for the liball.so library.
Let's say out test file contained the following code:
Now we have created an executable file named "test" which once executed would give us the output "Hello World" this is because the -l flag in our compiler went and searched through our library for the _puts function. We have now reached dominion of the libraries.
So what is the difference between static and dynamic libraries?
There are key differences between Static and Dynamic libraries.One key difference with static libraries is they can be used with a variety of programs, yet they are locked to one program during compilation time. Dynamic libraries are an entire separate file created outside of the executable program file. Because of this a program only needs to make a single copy of the library's file when compiling, a static library on the other hand needs a copy of the library file for every file when compiling.
This can have both advantages and disadvantages depending on which type of library you are using, dynamic libraries specifically can be susceptible to corruption or damage which would in turn cause the executable file to not be able to run. On the other hand it is very useful because multiple applications can all use the same library when running. Advantages can be found with Static Libraries as well like its speed at run-time, due to the object code being included with our executable the time it takes to run a program is cut significantly. This gives the programmer options on what to use and when, now we have become better programmers with this knowledge of libraries.
Onward on your journey towards clean, organized, coding!
Software Engineer | SharePoint and Power Platform Developer
2yThe information in this article is very clear, thanks.
Full Stack Engineer | Building Scalable Solutions | JavaScript, Python, React, Node.js, Tailwind, MongoDB, MYSQL, AWS Expert
2yThanks for this