Differences between Static and Dynamic Libraries

Differences between Static and Dynamic Libraries

In this article I will explain what are static and dynamic libraries, how they work, how to create them and how to use them.

Why use libraries ?

When you are developping programs they tend to grow larger and larger.

This can be a big problem as a developper and to having a better way to manage our projects we can used libraries.

In the Unix and Linux operating systems, a library is a file containing one or more objects files. which consist of object code that is usually the output of a compiler (C as source language) or an assembler (assembly language as source language). 

We can create two kinds of libraries:

  • Static library
  • Dynamic (shared) library


How to Create and Use a Static Library (Linux)

Create a Static Library :

For creating a static library, you need to use the command ar for 'archiver', indeed the object files are turned into a library in the form of an archive.

This command can also be used to list the names or modify the object files containing in the library. Ar will generates an archive file, ends in .a, which is a file containing other files : a static library.

In order to create a Static Library we need first to generate object files, this can be done with the following command :

gcc -c *.c

This command tells the compiler to generate object files for all the .c files in the current directory.


Now that we have our object files we can archive them as follow :

ar rc libholberton.a *.o

The *.o wildcard tells the compiler to compile all the object files .o .

The -r flag tells to replace older object files with the new object files.

The -c flag tells to create the library is it does not already exist.

This command will create a static library called libholberton.a.


After have created a library it is necessary to index it. This is done with the command ranlib. ranlib generates an index to the content of a library and stores it in the archive.

ranlib libholberton.a

Also, you can use the command nm to list the symbols (again, functions, variables and the like) from an library.


Use a Static Library :

To use a static library in a program we need to add the library name with the l option to the list of object files when we compile it.  

gcc main.o -L. -lholberton -o prog

The -L flag specifies the path to the library .We can use -L. inorder to point to the current directory.

The -l option combined with holberton tells the compiler to look for a static library called libholberton.a .


How to Create and Use a Dynamic Library (Linux)

Create a Dynamic Library :

To create a Dynamic Library in Linux type the following commands:

gcc *.c -c -fPIC

It will generates one object file .o for all source files .c in your current working directory.

The -c option ensures that the source files will be compiled without be linked.

The -fPIC flag ensures that GCC produce a Position Independent Code. It means that the generated machine code is not dependent on being located at a specific adress, the dynamic library can be loaded at any adress in memory.


Now that you have generate your object file, type the following command to create your dynamic library :

gcc *.o -shared -o libholberton.so

The naming convention for dynamic libraries is such that each dynamic library name must begin with lib and end with .so . In the example below, my library is called libholberton.so .

The *.o wildcard tells the compiler to compile all the object files .o and the -shared flag tells the compiler to produce a dynamic library which can be linked with other objects to form an executable.


Finally, create the environment variable LD_LIBRARY_PATH which allows to give a path list to folders containing the dynamic libraries.

export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

Moreover, the ldd command allow you to list the shared libraries used by a program.


Using a Dynamic Library :

Now that you have created your Dynamic Library libholberton.so, you want to use it with other programs. For example you have a code test.c with uses a fonction defined into libholberton.so .

You can compile your source code test.c as follows :

gcc -L test.c -lholberton -o test

The -L flag tells the compiler to look in the current directory for the library files.

The -l option combined with holberton tells the compiler to look for a dynamic library called libholberton.so

This command will so generate an executable file called test and you can execute it typing : ./test .


Differences between Static and Dynamic Libraries

The dynamic library are .so files. Dynamic linking links the libraries at the run-time. Thus, all the functions occupy a particular place in memory space, and each program can access them without having multiple copies of them.

The static library are .a files. All the code related to the library is in this file, and it is directly linked to the program at compile time. A program using a static library takes copies of the code it uses in the static library and integrates them into the program.


Advantages and Drawbacks

In the case of static libraries, if modifications are made to any of the files, the executable file will have to be recompiled instead in dynamic libraries the executable will not have to be recompiled.

Also the dynamic libraries are faster to execute because their code are already in memory, for the static libraries it takes longer because loading into the memory happens every time while executing.





To view or add a comment, sign in

More articles by Ophélie Fourbet

  • Python - How object and class attributes work ?

    The Python programming language was created by Guido van Rossum in 1991 and is made available under a free license. It…

  • What happens when you type ls -l in the shell ?

    You know how to code but you've never wondered how a shell works from the inside? Or you have just started coding and…

    2 Comments
  • Two's complement

    In a computer all is stored in bits : 0 and 1 values, and usually numbers are stored in one or more bytes, a byte…

  • What happens when you type gcc main.c ?

    When you write a C source file, it is just text, but your computer does not understand these instructions. Your…

  • What is the difference between a hard link and a symbolic link?

    In Unix : "everything is a file" describes one of the defining features of Unix-like operating systems and a file is…

  • What happens when you type ls *.c

    You have just started coding and you want to know what happens when you type those character ? To understand the full…

Others also viewed

Explore content categories