Difference between Static and Shared libraries

Hector Orozco
5 min readSep 7, 2020

In programming, a library is a collection of pre-compiled pieces of code that can be reused in a program. Libraries simplify life for programmers, in that they provide reusable functions, routines, classes, data structures and so on
which they can be reused in the programs.

Static Libraries : A Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static.
They are usually faster than the shared libraries because a set of commonly used object files is put into a single library executable file. One can build multiple executables without the need to recompile the file. Because it is a single file to be built, use of link commands are simpler than shared library link commands, because you specify the name of the static library.

Shared Libraries :
Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files.
These 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.

  • Why using libraries in general?

While programming sometimes we perform a specific operations in C, those operations are regrouped in a function, in order to gain time and regroup our needed functions for later use, we do need to use libraries.

A library is a file where the functions are regrouped together. So basically it’s just an archive of object files of each function.

How do they work ?

- Static Library :

We need to compile the source files of functions .c to object files .o

A static library is like an archive file, the extension is .a and the files contained inside are .o but it depends sometimes on the platform you are using.

When we will generate an executable, the linker will try to resolve the referenced symbols and locate in which object they are defined, then it will link them together.

- Dynamic Library :

Dynamic library are loaded in the memory during the runtime of the program, while static library are linked to the program during the compilation.

Once loaded the dynamic library can be used by any program, that’s why its called a shared library.

What are the differences between static and dynamic libraries

- Static Library

Static Libraries are .a files relating external functions and variable which are directly linked into the program at compile time.

A program using a Static Library is going to take copies of code and make them a part.

- Dynamic (Shared) Library

Dynamic (Shared) library is a .so file dynamically including the address of the library, it links the shared library at the run-time.

All the functions are in a place of memory and every program is allowed to access them without having to use a multiple copies.

What are the advantages and drawbacks of each of them

- Linking time / Process

In Static Library the linking time is the last step of the compilation process after the program has been added to the memory. The operation is performed by the linker.

In Dynamic (Shared) Library the linking time takes place when the executable file and libraries are added to the memory. The operation is performed by the operating system.

Compilation and external file changes

In Static Library, if we made some changes to external files, we need to recompile the executable file, because the library is basically inside the executable file.

In Dynamic (Shared) Library, even if we made some changes to external files, we dont need to recompile the executable file.

Size, time and compatibility

Static Library is much greedy of size, because as mentioned before the library is basically inside the executable file.

Also it takes more time to execute, because the system needs to load the static library into the memory in every execution.

No compatibility issue, because every resource that we need is in our executable file.

Dynamic (Shared) Library, is smaller, because the shared library is kept in memory and it is not stored inside the executable file.

It is faster than static library because the shared library is already in the memory.

Compatibility is not a problem if shared library is in the system, otherwise our program will not work, so we can say that shared library is dependent.

How to create them

Static Library

Step 1 : Create object code

in order to change the source files of functions .c to an object files

gcc -c *.c

Step 2 : Create library (archive file)

In order to create an archive file composed of all object files that are in the command

ar rc libname.a *.o

Step 3 : Optimisation

In order to index the library and make compilation runs faster

ranlib libname.a

- Dynamic (Shared) Library

Step 1 : Create object code

in order to change the source files of functions .c to an object files

We use the option -fPIC

gcc -Wall -fPIC -c *.c

Step 2 : Create library (archive file)

In order to create an archive file composed of all object files that are in the command

gcc *.o -shared -o liball.so

Step 3 : Export

To make our program works with dynamic libraries is through the $LD_LIBRARY_PATH enviornment variable.

It’s better to verify if the variable is already defined by using this following command:

echo $LD_LIBRARY_PATH

Also we are going to update it with our new library path with the following command:

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

or

setenv LD_LIBRARY_PATH /path/to/library:${LD_LIBRARY_PATH}

How to use them

In order to use the library in a program compilation, we need to add the libname as follow :

gcc main.c -L. -llibname -o nameofprogram

We assume our main() function is inside main.c and we need to use the library libname to create the program nameofprogram

--

--

No responses yet