contact@embeddedgeeks.com
Embedded World

Libraries


Libraries are of two types

  1. Static
  2. Dynamic

Both has their own pros & cons based on their applications.
It also depends on where we want to use those libraries.

Advantages of using libraries

  • It helps us in levreaging a common code base across the platform (i mean the system).
  • We can build modular applications by the help of libraries.
  • Can declare common parts of program as libraries, so that we need not to write the same things again and again in every programs.

Static Libraries

Static libraries contains all the required dependencies on a single binary.
By doing so the size of the binary increases, which in turn will occupy more space as compared to dynamic libraries and its not good for space constraint embedded devices.
But it also has a benefit that it removes the depedencies on other’s code base, and its a independent executable.

How to create static libraries on your linux system.

Step 1: Write your code.
eg.

#include <stdio.h>        
int main()
        {
            printf("Embedded Geeks\");
            return 0;
        }

Step 2: Compile the above program using below command.

    >> gcc test.c -o test_static --static

It will give you the o/p as a standalone executable binary.

How to verify Static executable.

The utility “ldd” in linux lists the linked libraries to a particular executable.
You can check your static code by below method.

ldd ./test_static

You will get this message on console: “not a dynamic executable”

You can also check the used SYMBOLS on the executable.

nm -C test_static

It will have a huge list of appended symbols, since it will also import the required libs from the toolchain/compiler(gcc in generic).

You can also check the size of the generated executable using below command and notice that the size of the static executable is larger than the dynamic executable.

size ./test_static

Dynamic Libraries

These are the libraries which are linked in a dynamic manner to your code/executable, they are usually linked using the softlinks.
The executable generated by using dynamic libraries are smaller in size as compared to the static ones because they do not import all the required libs instead they create links to the called libraries.

When you compile a code without passing a static flag in gcc, by-default it is compiled as dynamic executable.

To verify the dynamic nature you can use the below command.

ldd ./test_dynamic
It will list all the linked libraries.

The size of a dynamically linked executable is less but its not portable in nature.
While porting this executable from one system to another you will have to make sure you import its required libraries as well.

Console Output: