What s a library? and what difference between static and dynamic
What’s a library ?
A library is a collection of pre-compiled pieces of code called functions. The library contains common functions and together, they form a package. Thus file are object file and generaly finish by “.o” in which there is the function definitions in binary form.
How to link a library to our program?
When our compiler, compile a file it will follow thus 4 step :pre processing, compiling, assembling, linking. We have already talk about the first 3 one in an other article. Now let’s talk about de linking step.
At that step, the compiler will link a library to our code in order to make the library available for our program.
The first advantage of using a library is the ability to distribute an executable file with a bunch of function. Then anyone with that file, will by able to use the different functions and it will be easy to compiled. That is really good.
The second advantage is by using a library, it will give you the opportunity to organise your function. For example if you create a library with all your string’s function and an other library with all your mathematic’s function. You understand that by organising your library in functionality it will be much more easier for you to know where to look for.
That type of structure is already used by the C langage in the what we call a standard library. That library is organized in functionality like <math.h> for math function, <string.h> to manipulate character etc…
There is two ways to link a library. The first one consist in copying the entire library in our program, it’s will use a static library. The second way consist in simply created a link to the library file without copying It, it’s a dynamic library.
Static vs Dynamic Library?
The first difference is the portability :
In a static library is the portability. As there is all the code in the library, you can use it everywhere and you are sure to use exactly the same function. The counterside is you will not be able to modify the compiled file easily
In a dynamic library : The code will not be in the library, you will not be sure to use exactly the same function. The advantage will be that you can modify it easily.
The second difference is the size of the file.
A static library have all the code in it so the size of the file will be bigger. At the constraty in the dynamic library, the code is not in the file, so the file size will be smaller.
The third difference is compilation step.
in a dynamic library, as the code live inside the library every time we will have to compile our file. At the contrary, of the dynamic library the code live outside, thus we can modify it without need to re compile it.
The four difference is the accessibility.
In a static library only one program wil accesss to the library at a time. In a dynamic one, lot of running applications can use the same library without the need of creating a copy of the entire library.
The last difference is the speed execution time.
Generally a static library will execute faster because its binary is included in the executable file.
How to create a static library?
I know i have already talk about it in an other article but lets do a small refresh:
To create it, we simply have to use the command :‘ar -rcs <library name>a *.o’
So we use ar with flag rcs in order to create a new static library file.
Then if we want to see the list of symbol in our library we can yes the nm command .
nm lib <my_library>.a
Then if we want to optimize the use of a static library we can compile with the ranlib command. it
ranlib lib<my_library>.a
It will index all the header which will improve the loading time.
How to create a dynamic library.
To create a static library, we need Object file. generaly thus object finish by “.o”
Step A1 : gcc *.c -c -fpic
So we will compile all the program with .c source. Then we will apply the flag -fpic. Thus we will tell to the compiler to stop and return the object file.
Step A2 : gcc *0 -shared -o liball.so
Now we will compile all of our .o file by using the -shared flag. Our library is the .so file.
Then, in the future, we will be able to use this liball.so to compile our program. Our compiler will idendity a library by his name . It has to beging by “.lib”
Step A3 : export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
As a dynamic library do not contain the code, we will have to say to our program where to look to find the library file. Thus we have to add this location to the environemental variable LD_:IBRARY_PATH.
Compile our program :
Step B1: Compile our program with the library with : gcc -L. -lall -o my_program main.c
To compile a program we have to tell to our program where to find our library with de -l ( it will look at all .so and ‘lib’ begning file). Here we use the -L in order to find our lib in the current directory.
To manage library, we can use some command like :
ld config.
ldconfig allows us to create update and remove the cache and links to the latest shared librarie. It’s speicified on /etc/ld.so.config. This command will check the header and file name and determine the good version.
ldd : Telle to a program what library has to be run. By using this command, we can detect if there is some missing functions or objects or compatibility problems.
Thank you for reading