The differences between static and dynamic libraries.
Hello again, here we are writing and reading about libraries once again, in this case I will be explaining you the differences between Static and dynamic libraries.
In this occasion I won’t focus too much on static libraries as we have already touched the subject in a prior post which you can read here. “Go on, I will let you finish reading so we can continue”.
Now that you finished and have a clear understanding of what static libraries are, lets start with the topic at hand.
So why do we use libraries?
As said in the prior blog,
“Libraries are useful as they can handle lots of information which is indexed, making it easier and faster to access.”
In C programming there are times when we need to use and reuse several functions, so it is useful to have them indexed to facilitate the access to its object files (functions, variables, etc.). This makes programming easier as you have all the content you need on the tip of your fingers without having to go back and forth searching for the information you need.
How to create and use static libraries.
To create a static library we have first to compile the functions we are going to use. To achieve this we compile them similar to the process we did by using the gcc compiler as follows:
The step above will compile the files and create a dot “o” (.o) file.
The next step will be to create the library by using “ar” that means archiver and the syntax would be the following:
We use the command “ar” to create the library along with the commands flags -r, which tells the library to replace existing files with the new ones and -c and in this case creates the library that is going to be called “libc”.
As said before at the beginning libraries are indexed for easier and faster access to the information in it. The indexing process is in general done by the compiler but if there is a need to do it manually the command used is ranlib like the example below.
Indexing library content
Now we have finally created a static library that we can use to speed thing up in our coding process.
How to create and use dynamic libraries.
Just as with static libraries it is necessary to compile our files in order to create the object file (.o), as follows:
It is required to use the -fPIC option when creating object files to be used in dynamic libraries. -fPIC stands for “position independent code”, the -c converts .c files into .o or object files.
The following step is to create the dynamic library by adding the object files to it and this is done with the following command:
To create the shared object (.so) -share is used to create and link other objects to create the executable.
With all that done we only have one more step to go with and that is to install it in the standard location.
Differences between static and dynamic libraries.
Static Libraries
“- Static library has functionality that bound to a static program at compile time. Every static program has its own copy of library.”(1)
Advantages:
The static libraries are faster to load becuase they are writen into the program. Since the source code contains the code from the library, there are no compatibility issues.
Disadvantages:
Object codes are larger and take enough space to become an issue. Static Libraries can’t be updated, so in order to do an update, a new compilement has to be done.
Dynamic Libraries.
“- Dynamic libraries are loaded into the memory and binds at run time. The external functionality is accessed at runtime. This process reduces the overall footprint of memory.” (1)
Advantages:
In the case of memory usage, they will save space in disk as they will only be writting one memory adrees in the object code. They are also easier to update, there is no need to again complie all the files, only the code in the DInamyc library.
Disadvantages:
Can make progrmas run a little slower. Some compatibility issue may arise when a library is changed.
References:
1: https://www.careerride.com/Linux-Static-dynamic-library.aspx