“A library contains history, is where history can begin, and is a great place to be.”
Hi there, In my blog, we will try to solve the Differences Between Static And Dynamic Libraries. So, let’s get started!!
First, we can ask some questions:
Why using libraries in general?
How to create shared libraries in Linux? and How to use them?
Differences between static and dynamic libraries.
What are the advantages and drawbacks of each of them?
It is known that the purpose of libraries in C are to order the functions we build and work with. To avoid mistakes and to be efficient, programmers need to be organized in their work. Libraries have gone through rigorous testing and work, so they have been optimized to have a good performance.
Basically, we have two types of libraries: static libraries and dynamic libraries. In this post will be discussing the differences between them.
I will give you a brief demonstration on how to create a static library, you can find all the information about it in my previous blog that I gave a detailed explanation of how to create and use static libraries you can see it in the link below:
What’s is a static library?
A static library (also known as an archive) consists of routines that are compiled and linked directly into your program. When you compile a program that uses a static library, all the functionality of the static library that your program uses becomes part of your executable. Static libraries use the extension a the a stands for archive.
First thing you must do is create your C source files containing any functions that will be used and a header file named ‘name.h’ that contains the prototypes of these functions.Then we execute the following commands and we have already created our library:
gcc -c *.c
ar -rc libholberton.a *.o
We can use the
-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:
Now our static library “libholberton.a” is ready to be used. we can use it in a program. we create a C source file named main.c
gcc main.c -L. -lholberton -o main
-L : Specifies the path to the given libraries (‘.’ referring to the current directory)
-l : Specifies the library name without the “lib” prefix and the “.a” suffix
now we will just run our program.
What are Dynamic Libraries in C?
Shared libraries (also called Dynamic libraries) are linked dynamically simply including the address of the library (whereas static linking is a waste of space). Dynamic linking links the libraries at the run-time. Thus, all the functions are in a special place in memory space, and every program can access them, without having multiple copies of them.
How do libraries work?
How to Create a Dynamic Library (Linux)?
First thing you must do is create your C source files containing any functions that will be used and a header file named ‘holberton.h’ that contains the prototypes of these functions.Your library can contain multiple object files.
After creating the C source files, compile the files into object files. so we tell GCC to do this using -c
gcc *.c -c -fPIC
This command essentially generates one object file
.o for each source file
-fPIC flag ensures that the code is position-independent. This means it wouldn’t matter where the computer loads the code into memory.
Next, To create a Dynamic library type the following command:
gcc *.o -shared -o libholberton.so
* tells the compiler to compile all the
.o files into a dynamic library which is specified by the
The naming convention for dynamic libraries is such that each shared library name must start with
lib and end with
Then, We use the “ldd” command that prints shared object dependencies.
By executing the following command: we’ll export the path for libraries so that programs know where to look for them. we add the library path to the environment variable:
To verify that you did it and have the right functions as dynamic symbols you can use:
nm -D libholberton.so
Using Dynamic Libraries (Linux):
I will show you an example that illustrate how we can use static libraries:
We’ve created a shared library called
libholberton.so and now we want to use it on a file called
Then, you can compile it by typing the following:
gcc -Wall -Werror -Wextra -pedantic -L. 0-main.c -lholberton -o len
Note that the name we gave to the library in this example was ‘holberton’.
The -L after the library name telling the linker that the library might be found in the current directory. The -l option is to tell the compiler to look for the library.
Now you can use your
len executable file, which prints out the length of the string “Holberton” wich is 9.
And Finally, we don’t forget to mention why we use ldcongif:
So, ldconfig creates the necessary links and cache (for use by the run-time linker, ld.so) to the most recent shared libraries found in the direc- tories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/usr/lib and /lib).
Differences between static and dynamic libraries: (advantages and drawbacks of each of them)
And that’s all, hope you enjoy it! Thanks for reading!!