Differences between dynamic and static libraries

Why choose one over another (Linux)

Rodrigo Zarate Algecira
3 min readSep 24, 2021

Dynamically linked Shared Object libraries: .so | Static Libraries: .a

  • Why using libraries in general

Developing software, you will find that you have to create functions that can be useful somewhere else. Imagine a login form and the clockwork behind, or something that measure the length of a string. To avoid repeating over and over pieces of code, you can take advantage of libraries. Write some good code that works very well doing a task and use it multiple times in projects.

There are two Linux C library types:

* Static libraries

* Dynamically linked shared object libraries

  • How do they work

Libraries add new capabilities to your software. The Linux executable files are machine code. That means that you must translate .c files to machine code. The process of translation is known as “compilation” and involves four main tasks. How you define the link to your libraries makes the difference between a shared and a static library.

  1. C Preprocessor: Processes Preprocessor directives like #define and removes comments.
  2. Compilation: Transform the code into objects, create a new yourfile.o from your code in yourfile.c
  3. Assembler: Transform the object code into binary machine code
  4. Linking: Connection of libraries

The object files for static or a dynamic shared library involve different commands and flags.
Use it to create an object file to be included in the static library :

$ gcc -g -O -c file1.c

Use it to create an object file to be included in the dynamic library :
PIC stands for Position Independent Code

$ gcc file1.c -c -fPIC

Bear in mind that the $ symbol represents the terminal prompt and is not part of the command.

In both cases, you end up with a file named file1.o but are not the same inside. The first case contains all the object code the second one have only references.

  • How to create them (Linux only)

Once you have your object code files, you can now create your library.
Dynamic shared libraries are supposed to be in a standard location. If you have to use another place, you must tell the linker where the files are.

Use this command to accoplish this task:

$ export LD_LIBRARY_PATH

To create a STATIC library from your object code use this command below:

ar rcs file1.a file1.o

To make it simple we use here only one file, but you can include more as you need.

To create a DYNAMIC library from your object code use this command below:

gcc -shared -o libfile1.so file1.o
  • Library naming conventions:

lib XXXX .so | XXXX .a

The pattern for dynamic libraries is lib + filename +.so (Shared Objects)
The one for static libraries is filename +.a

  • How to use them

To use a STATIC library use this command below:

gcc -o main main.o -L. -file1

That will create the “link” of the objects in the library inside your code by coping it directly in your main file.

To use a DYNAMIC library you must tell the interpreter where the files are located, to acomplish this task use this command below:

$ gcc -L/<path>/file1 -Wall -o main main.c -lfile1

Once the interpreter is instructed you can proceed to export.

$ export LD_LIBRARY_PATH=/<path>/file1:$LD_LIBRARY_PATH

That will allow this <path> to be available for the linker at run time.
Of course, you have to replace <path> with your actual path to your library.

  • What are the differences between dynamic and static libraries

Dynamic Vs. Static
The STATIC libraries are copies of the objects inside your code on the other hand DYNAMIC libraries are separated files that can be part of multiple programs at run time.

  • What are the advantages and drawbacks of each of them

You have to think wich one is the best for your programming needs, that’s why theres two ways to load libraries in your files.

Resources

--

--

Rodrigo Zarate Algecira

In the path of Programming #AgriculturaUrbana $rodrigozarate Instagram: agromerlin