Static or Dynamic Libraries, which one to use…
To be able to decide which one to use, we have to know exactly what a library is…
A library is a collection of pre-compiled pieces of a code called functions. The library contains functions that together they form a package, that is ehat we call a library. Functions help teh programmer from rewriting the code several times, jsut by calling them they have the piece of code they need.
Libraries need object files created by the “-c” gcc flag and en in .o
Librarues are not executable, they onlu play their role at run time or compile time. In C we are going to see two types of libraries, Static and Dynamic
Diference between Static and Dynamic Libraries
Advantages and Disadvantages of Dynamic Libraries
- It only needs one copy at runtime. It is dependent on the application and the library being closely available to each other.
- Multiple running applications use the same library without the need of each file having its own copy.
- However, what if the dynamic library becomes corrupt? The executable file may not work because it lives outside of the executable and is vulnerable to breaking.
- They hold smaller files.
- Dynamic libraries are linked at run-time. It does not require recompilation and relinking when the programmer makes a change.
At compile time, applications utilize static libraries. All the copies of the functions get placed into the application file because they are needed to run the process.
Advantages and Disadvantages of Static Libraries
- Static libraries resist vulnerability because it lives inside the executable file.
- The speed at run-time occurs faster because its object code (binary) is in the executable file. Thus, calls made to the functions get executed quicker. Remember, the dynamic library lives outside of the executable, so calls would be made from the outside of the executable.
- Changes made to the files and program require relinking and recompilation.
- File size is much larger.
How to create libraries.
For both of them you should include a header file that contains all your functions prototype.
For dynamic libraries you should write the following command:
gcc -g -fPIC -Wall -Werror -Wextra -pedantic *.c -shared -o liball.so
The “*.c” takes all the .c files in the current directory and make a library call liball.so. The -fPIC flag allows the following code to be referenced at any virtual address at runtime. Object file get compiled by using the -shared flag. The compiler will later identify a library by searching for files beggining with “lib” and ending with “-so”
The program needs the path in order to look for the library files. So, you must type the following command to add that location to the environment variable called
When using the dynamic library, type the following command:
gcc -g -wall -o app app.c liball.so
For Static Libraries you should write this command:
ar -rc name_of_libary.a our_function.o
Using the command ar to create it, with rc option, followed by the name of the library we want to create and finally the name of the functions we want to add to it.
To make it easier if we want to add al the .o files we have in the current directory, our last argument could be *.o.
Next step is indexing, to do that we type:
If we want to compile any function that contains another one inside, we could get an error if we dont call the libary that we need to use.
We should add some options to our gcc command to make it work…
gcc my_program.c -L. -lname_of_library -o my_program
- First of all -L says “look in directory for library files” if we add a dot it means the current directory.
- -l says “link with this library file followed the name of our library, ommiting the .a prefix of it.
- And at last -o option followed by teh program we want to compile.
By making all this we are going to be able to compile our programs with our own functions and make the work perfectly.