The differences between static and dynamic librarie

Salmen Zouari
5 min readDec 16, 2019

Why using libraries in general?

A library is a set of code that was previously written, that can be called upon when building your own code. It is work that was previously done by someone else that you can now make use of, without having to do it yourself. And you can make use of it in your own code, legally, as it was written to be used by others. Now different libraries will have different restrictions on fair use, but this was code what wasn’t designed to just stand alone, but to be used in someone else’s code.Using them saves time, removing the need to rewrite code multiple times. Libraries, like functions also save time in that they make functiones reusable in multiple programs.

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries on the other hand, exist as separate files outside of the executable file.

How libraries work?

Once the libraries are created, they can be linked with the file containing the main function, or entry point, with gcc. Then, the code of the functions used in the program will be linked into the executable program, and it will be ready to run!

How to create them (Linux only)

Static Library

Let’s use as an example the file “my_file.c”.We need to create object files first. This is done with the following commands:

$ gcc -c my_file.c

The ‘-c’ option makes sure the compilation process stops before the linker, and creates the ‘my_file.o’ file

Now we can create the ‘mylib’ library with our object file. Here’s how:

$ ar rc libmylib.a my_file.o

We use the command ar to create a ‘.a’ file (a stands for archive)

Dynamic Library

To create a dynamic library in Linux, simply type the following command: gcc *.c -c -fPIC and hit return. This command essentially generates one object file .o for each source file .c . The -fPIC flag ensures that the code is position-independent. This means it wouldn’t matter where the computer loads the code into memory. Some operating systems and processors need to build libraries from position-independent code so that they can decide at runtime where they want to load it into memory. The -c options just ensures that each .o file isn’t linked yet.

Next, type in the following command: gcc *.o -shared -o liball.so (substitute your desired library name with all) and hit return. The wildcard * tells the compiler to compile all the .o files into a dynamic library which is specified by the -shared flag. The naming convention for dynamic libraries is such that each shared library name must start with lib and end with .so . Other than that though, let your imagination run free when considering names for your dynamic libraries.

Finally, we’ll need to export the path for libraries so that programs know where to look for them by executing the following command: export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

How to use them (Linux only)

Dynamic Libraries

The point of creating a dynamic library is to use it with other programs. You can compile your code as follows:

gcc -L test_code.c -lholberton -o test_code

In the above command it is worth noting that your source code, test_code.cin this case, needs to be listed before the -l flag. The expression, -lcombined with holberton tells the compiler to look for a dynamic library called libholberton.so, while the -L flag tells the compiler to look in the current directory for the library file. This is why it is important to use the standard format for naming that I described earlier. For instance if test_code.c was the following:

#include "holberton.h"
int main(void)
{
_puts("Hello World!");
return (0);
}

Typing and executing gcc -L test_code.c -lholberton -o test_code would generate an executable file called test_code. In order to accomplish this, the compiler looks through the library that is specified with the -l flag for the _puts function object code. Executing test_code like so: ./test_code would give us the following output: Hello World!. Now that you know how to create and use dynamic libraries, go and conquer the world!

Static Libraries

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 :

$ gcc -L. main.c -lmylib -o test

The ‘-L’ flag tells the compiler where it needs to look for the library, so in this case where the library is in the current working directory, we just use a dot. the ‘-lmylib’ tells the compiler to link the code in main.c with the code in the library my_lib. Finally, the ‘-o’ flag allows us to give a name to the executable, in this case it will be ‘test’.

What are the differences between static and dynamic libraries?

Static libraries increase the size of the code in your binary. They’re always loaded and whatever version of the code you compiled with is the version of the code that will run.

Dynamic libraries are stored and versioned separately. It’s possible for a version of the dynamic library to be loaded that wasn’t the original one that shipped with your code if the update is considered binary compatible with the original version.

what are the advantages and drawbacks of each of them?

When considering the advantages and disadvantages of Static and Dynamic libraries, we may want to consider size, speed, and updates. Since the actual code from Static libraries is written into your program, when it comes time to run the program, the code is already there, and so, your program may run slightly faster than programs that must, at run time, go and retrieve code at a given memory address. However, your object code files will be larger when using Static libraries, and if you have many of them, the added space taken up on the disk may be an issue to consider. Another issue with Static libraries concerns updates. If you need to update material in a Static library, the only way to update all the programs that already have the corresponding code written in them, is to re-compile those files. If they are few in number, this may not be an issue, but if there are many, Dynamic libraries may be preferable. When it comes to compatibility, Static libraries offer an advantage: since the source code contains the code from the library, there are not compatibility issues that arise from updates to a Dynamic library. So now, when we consider Dynamic libraries, we can see their advantages and disadvantages. A Dynamic library, while it may make your program run a little slower at run time, will save space on your disk because you will only be writing a memory address into your object code instead of the much longer code itself. And when you need to make updates, instead of recompiling all your files, you simply need to update the code in your Dynamic library. The programs that use it store only the memory address, so one update will suffice for all your programs.

--

--