Hello, in this blog we are going to describe the differences between static and dynamic libraries.
We are going to explain Why using libraries in general, How do they work, How to create them, How to use them, What are the differences between static and dynamic libraries and What are the advantages and drawbacks of each of them.
what are libraries and why use them ?
Libraries are files with a bunch of functions in them.
In general, they are created from many library source files, and are either built as archive files (liholberton.a
) that are statically linked into executables that use them, or as shared object files (libholberton.so
) that are dynamically linked into executables that use them.
Libraries allow us to reuse functions so we can save time and remove the need to write the code multiple times.
We have 2 type of libraries:
- Static library
- Dynamic library
What are Static libraries and How to create them?? (Linux only)
This is a brief demonstration on how to create a static library, for that matter I have written a blog previously named Static libraries in C check it for more informations.
So static libraries are simply a collection of ordinary object files; conventionally, static libraries end with the “.a’’ suffix. This collection is created using the ar (archiver) program.
- To create a static library we must first have all the .c files with the functions that we want our library to have. After this, we create an .h file with all the prototypes of the functions. Then we execute the following commands and we have already created our library:
$ gcc -c *.c
$ ar -rc libholberton.a *.o
$ ranlib libholberton.a
- we can use the
ar
option-t
to see the contents of our library,
$ ar -t libholberton.a
- We can also see the symbols in our library, using the command “nm”, which lists each symbol’s symbol value, symbol type, and symbol name from object files.
$ nm libholberton.a
- To use the static library call on the library during the linking phase of program compilation so we can use them after creating a program.
$ gcc main.c -L. -lholberton -o main
- Now, you are able to run the executable program.
$./main
What are Dynamic libraries ?
They are similar to Static libraries in the sense they both are built form several object files (.o).
However, unlike the archive the object files are properly linked together in a dynamic library to form one single piece of object code.
How to create a dynamic library? (Linux only)
- First of all we need to convert all the .c files that you want to save in the library into object files
$ gcc -c *.c -fpic
pic — stands for position independent code
- Then we have to tell the compiler to create a shared library using the flag “-shared” and we call all object files “.o” and by convention a library should start with “lib” and end with the “.so” extension shared object.
$ gcc *.o -shared -o libholberton.so
- Now let’s see an example on how to create a shared library
- We can also see the symbols in our library, using the command “nm”, which lists each symbol’s symbol value, symbol type, and symbol name from object files.
$ nm -D libholberton.so
How are dynamic libraries used?(Linux only)
We have created a dynamic library and can now use it.
- First you need to write a program:
“holberton.h” is the header file that contains all C used functions prototypes.
- When we compile the file we call the library:
$ gcc 0-main.c -L. -lholberton -o len
‘-L’ specifies the library path.
‘-l’ goes before the library name.
- Then, We use the “ldd” command that prints shared object dependencies.
we can see that the “libholberton.so” was not found.
- Add the library path to the environment variable:
$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
- Now run the program ./len :
The length of the string stored in 0-main.c file was found now.
NB:
the system knows where to find each library by the “ ldconfig” that creates the necessary links and cache to the most recent shared libraries found in the trusted directories.
Difference Between Static Library and Shared Library and advantages and drawbacks of each of them:
Static linking is the process of copying all library modules used in the program into the final executable image. In contrast, dynamic linking is the process of loading the external shared libraries into the program and then binds those shared libraries dynamically to the program.
Thus, this is the main difference between static linking and dynamic linking.
Conclusion
In brief, static and dynamic linking are two linking mechanisms. The main difference between static and dynamic linking is that static linking copies all library modules used in the program into the final executable file at the final step of the compilation while in dynamic linking, the linking occurs at run time when both executable files and libraries are placed in the memory.