Differences between Static and Dynamic libraries

Erika Osorio Guerrero
6 min readMay 5, 2020

--

First of all let’s see what is a library and why it’s important to use them:

A library is basically just a file but it’s not executable and that is a key difference from processes and applications. It have a collection of pre-compiled resources, used by computers programs, such as functions, classes, configuration data, documentation and so on.

Why and When to used Libraries: Libraries provide the user the benefit to used a variate of files that can be reused in different programs. For example If we were going to use the same functions in different programs, we will just write it once and reuse it instead of having hundreds of copies of the same function, one in each program.

What is a Static Library

A static library is a collection of objects files that are linked into the program during LINKING phase of compilation. (Not important during run time); So only the program’s executable file is need it in order to run the program.

What is a share or dynamic library

A shared library or shared object is a file that is intended to be shared by executable files and further shared object files. Modules used by a program are loaded from individual shared objects into memory at load time or run time, rather than being copied by a linker when it creates a single executable file for the program.

So How compilation Works:

A dynamic library is linked into the program in two stages

  1. During compile time: the linker verifies that all symbols required by the program are linked into it or in one of its share libraries.
  2. During run time: When the program is started the Dynamic loader checks out the libraries that were shared with the program, loads them to memory and engage them in a copy in memory

Difference Between Static Library and Shared Library

How to create a static Library

  1. convert all source code you want to save as a library, into object files:

using “gcc” Gnu compiler colection which is the program for translating the human code into a machine language or binary code, will translate our files into executable files but using the flag -c will compile the file and will stop just before the linking its done, so we will end up with object files

$ gcc -c *.c

2. Now in order to create a static Library we have to use the [ar] “archiver” program and call all the object file “.o”.

$ ar *.o -rc libholberton.a 

The name of a library by convention should always start with “lib” and end with a “.a” extension. The -r option will add newer object files into the library if the library already exists. The -c option will create the library if the library does not exist.

3. Finally we can compile our programs with the static Library, running the following command:

$ gcc main.c -L. -lholberton -o alpha

This will create a program using the file “main.c”, and any symbols it requires from the “holberton” static library. Note that we omitted the “lib” prefix and the “.a” suffix when mentioning the library on the link command, that is because the linker identifies libraries files and attaches these parts back to the its name.

The “-L.” specifies the path to the library, and in this case, the compiler can find the library in the current directory. The “-l” will include the library files, and the -o will rename the executable file to alpha.

$ ranlib

It’s a useful program that adds or updates object files in a static library. Linkers can use static libraries when linking in order to provide symbols that the code needs in order to operate

$ ranlib libholbertonschool.a

How to create a dynamic library

  1. convert all the .c files that you want to save in the library into object files
$ gcc -c *.c -fpic

The “pic” stand for position independent code

2. To create a dynamic library we have to tell the compiler to create a shared library using the flag “-shared”

$ gcc *.o -shared -libholberton.so

Then call al object files “.o” and by convention a library should start with “-lib” and end with the “.so” extension shared object.

let’s see an example of creating a shared library

3. Finally we can compile our programs with the Dynamic Library, running the following command:

$ gcc 0-main.c -L. -lholberton -o len

This linker will look for the library holberton in the current directory, and will create a program using the file “0-main.c”, but will not place any files inside the resulting executable file len as difference with the static library. Note that we omitted the “lib” prefix and the “.a” suffix when mentioning the library on the link command, that is because the linker identifies libraries files and attaches these parts back to the its name.

The “-L.” specifies the path to the library, and in this case, the compiler can find the library in the current directory. The “-l” will include the library files, and the -o will rename the executable file to “len”.

$ nm

It is a useful command that display information and list symbols (symbol table) from object files

$ nm -D libholberton.a

Here an Example of listing the object files with its symbols and information:

T → The symbol is in the text (code) section.

B → The symbol is in the uninitialized data section (known as BSS ).

W →The symbol is a weak symbol that has not been specifically tagged as a weak object symbol.

D →The symbol is in the initialized data section.

U → The symbol is undefined.

$ lld

Check if the system locates the library properly and prints the shared libraries that the program or file required in order to operate

Here an example of the program 0-main.c being compile with the shared library holberton. Using the “lld” command with the name of the executable file, will display the libraries it needs.

As you can see the libholberton.so is not found and that is because when building a new shared library that is not part of the system we need to use an Enviroment variable call LD_LIBRARY_PATH.

LD_LIBRARY_PATH.

$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

It’s an environment variable, and contains a set of directories where libraries should be searched for first, before the standard set of directories. I this case we are setting the env. variable to be after searched after the current working directory. So now the Library can be found by the executable file.

Here an example using export for the the environment variable for Libraries

$ ldconfig

Is command creates update and removes the necessary links and cache to the most recent shared libraries found in the directories specifies in the command line.

That’s all for today and I hope you enjoyed the article.

--

--