Differences between static and dynamic libraries

what is a library?

A certain type of file that we can import or include in our program is known as a library. These files contain the specifications of different functionalities already built and usable that we can add to our program, such as reading from the keyboard or showing something on the screen among many others.

A library is a set of reusable functions.
In programming, it is a common practice to reuse resources and libraries are an essential part of it.

How do they work?

The process of going from .c source file to an executable (.exe) involves preprocessor, compiler, assembler, and linker. During the linking phase of the compilation, the object code that’s taken from the assembler does not yet have all the functions such as printf() from the <stdio.h> attached to create one executable.

Static libraries

Libraries are projects with specific methods or functions that you can add to other projects and complement them using their specific methods for a given solution. A static library should always start in the name with lib and end with the extension .a

How to create a Static library:

first we are going to use the command ar that together with the parameters r and c would be something like this

ar -rc libname.a

but before doing this let’s remember that the static library is going to save us the object files so let’s look at some example object files

with gcc and the -c parameter we get the extension .o which is the object file with *.c selection all the files that end in this parameter

Now we’re going to create the library and we’re going to link the object files, it would look like this :

After creating the library we have to index it by entering the command ranlib
let’s see your syntax

ranlib creates an index of the contents of libname.a and stores the index of libname.a. This is useful for linking and objects calling each other.

How to use them ?

a small example of how to use them is when compiling them put the -L and -l parameters let’s see this:

Here I am compiling my func.c file, then I use the -L parameter to indicate that the libraries can be found in the given directory. now the call of the library, before writing the name of the library put -l next to it to indicate the name of the library and write it without lib and also the extension .a

Dynamic Libraries

Are similar to Static libraries in the sense they both are built form several object files (.o). However, unlike the archive the object files are properly linked together (referenced by memory address of the functions) in a dynamic library to form one single piece of object code.

How to use them ?

gcc -Wall -pedantic -Werror -Wextra -c -fPIC *.c

  • converts source code (.c files) into object code (.o files) with the use of the -c flag.
  • -fPIC flag: means Position-independent code. Need by systems and processors to build libraries so they can decide where they want to load it into memory.

gcc -shared *.o -o libname.so

  • -shared flag tells the gcc that you want to convert the object .code (.o files) into a shared object files (.so) aka dynamic libraries in Linux and Unix computers.

If you want to peek into your shared object file to see what are comprised functions use:

nm -D libname.so

  • nm command list symbols from object files
  • -D flag refers to the symbols in the initialized data section.

Difference between static and dynamic libraries

Advantages and drawbacks

Static library advantage.

  • Zero compatibility issues.

Static library drawbacks.

  • The size is too large.

Dynamic library advantage.

  • Linking happen at run time.

Dynamic library drawbacks.

  • Loading time is more.

Never stop learning.

Software Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store