Static and Dynamic libraries
Imagine you have to run the program you worked for a long time, but in the process you created a huge amount of pieces of code. If you work in your pc it will be easy to use, but if you have to use them on another device, you should have to copy these files, risking losing one of them and asking why your code is not working properly, for these and all another situations there are libraries.
They are a collection of pre-compilated code that can be used all the times and in all the devices that are needed, without the complications of use the disorganized code, today i want to show the advantages and differences between static and dynamic libraries.
How do they work
The main objective of libraries is to reused program code, allows functions and structures been available for been used not only for the main program, but also any other that can used the data that contains the library.
But libraries don′t contain the original source code, to work they need the object files output by the assembler.
How to create them:
The command to create static libraries is the next:
Otherwise, the command to create dynamic libraries is the next:
This allows object files been ready to be compiled into a library.
* The -Wall, -pedantic, -Werror and -Wextra flags depend on build requirements, can be omitted if desired.
As you can see, the first difference when create the object files is that we have to use the adittional flag -FPIC
The importance of flag -FPIC is that allows libraries to work no matter where its located in memory in reference to the rest of the code, aditional, allows dynamic libraries been used by more than a program at same time, that′s doesn′t happen with static libraries.
This option allows the operating system to know that it's been used a global offset table (GOT), and all references to your address are relative to it, avoiding the loader would have to modify all the offsets itself, this allows code can be shared through multiple processes.
Next, We have to create the libraries, the dynamic library command is the next:
领英推荐
While for dynamic libraries, the following command is used:
The -shared flag compiles all .o files into a sharable library.
Otherwise the ar command archives the library using the header and prototypes that were created to build the library, this makes creating a static library much simpler.
How to use:
In both cases, this command allows compiler to know where to find the library where it needs to run the program.
With static libraries you can run your program form now, but in dynamic cases you have to do one last instruction:
This update the?LD_LIBRARY_PATH?environment variable and prepare our working directory to its existing value.
Differences & advantages and drawbacks:
Another advantage of dynamic libraries allows them to be modified without the need to recompile, while static libraries are forced to be recompiled if their code is modified, because it is "locked" in the executable.
Although this can be seen as a great drawback of static libraries, it is also a great advantage, because it avoids corrupting the code it contains, which can make it useless, that is why it is also an additional security resource compared to the dynamic libraries.
Aditional, due all code is inside the library, multiple calls to functions can be made more quickly an easier than a dynamic library.