Static library vs Shared library
On the previus post, we saw the Static libraries and a brief introduction to the Dynamic libraries, now let’s dig deeper and compare them a bit.
Let’s recap a bit, What is a library in Linux? In computer science, a library refers to a collection of precompiled pieces of code, these pieces are stored in object format (.o) and together they form a package called: library.
Why do we use libraries? On many occasions we want to reuse a code, but are you willing to rewrite code fragments or functions again? Actually, the way to save time is using a library that provides us with reusable functions, data structures, classes, etc. Let’s see our library as a store of functions that we already have and make use of this store every time we need it, isn’t it great?
“For instance, if you are building an application that needs to perform math operations, you don’t have to create a new math function for that, you can simply use existing functions in libraries for that programming language.” (“Understanding Shared Libraries In Linux”)
A library is not executable and that is a key difference with processes and applications. Libraries perform their function at run or compile time. In the C programming language, we have two types of libraries: dynamic libraries and static libraries.
Static library
(also known as a archive) consists of routines that are compiled and linked directly to 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. On Linux, static libraries usually have a .a (archive) extension.
Dynamic library
(also called a shared library) consists of routines that are loaded into your application at run time. When you compile a program that uses a dynamic library, the library does not become part of your executable, it remains as a separate unit. On Linux, dynamic libraries typically have a .so (shared object) extension.
How to create a static and dynamic library?
The creation of a standard library, you can see specifically in the previous blog. Now, let´s to see the Dynamic library
- Prepare source .c files that currently contain our programs that execute a specific activity, as you can see in the example there are four files.c
2. We need to compile our library code into an object file, using : (visit gcc compiler)
$ gcc -c -Wall -Werror -fPIC *.c
With the -fpic
or -fPIC
flag, gcc creates an object file with position independent code. This is usually necessary when creating a shared object.
As you can see all files with extension .c in the current working directory have been converted into their respective .o object files
3. No we use gcc with flags ‘-shared’, and of course ‘-o’ because we compile from an object file, to create a ‘.so’ file (so is for shared object).
$ gcc -shared -o libshared.so *.o
4. Now that we have our library, we have to use is to compile and create our program. Let’s say our entry point is in the ‘main.c’ file, and we want to name our program ‘test’. Here’s the command to compile the program with our library in both cases:
4.1 we have add a path to the library to the LD_LIBRARY_PATH
environment variable:
$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
4.2 One more way but less flexible is to embed the location of the library to the executable itself ,not to the loader, using -rpath like this:
$ gcc -L/home/username/test -Wl,-rpath=/home/username/test -Wall -o test main.c -lshared
How and why do we use them?
Now compile the main function
$ gcc -Wall -pedantic -Werror -Wextra -L. 0-main.c -lholberton -o len
and if everything happened well you can run your executable and you will see the output.