Linux Basics: Static Libraries vs. Dynamic Libraries

 

If you’re wondering how static and dynamic libraries work in C programs, here is the place where I will answer you this tricky question, let´s start!





What´s a Library in Linux?

A library is a collection of pre-compiled pieces of code called functions. The library contains common functions and together, they form a package called  a library. Functions are blocks of code that get reused throughout the program. Using the pieces of code again in a program saves time. 

A library is not executable and that is a key difference from processes and applications. Libraries play their role at run time or compile time. In the C programming language, we have two types of libraries: dynamic libraries and static libraries.

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries on the other hand, exist as separate files outside of the executable file.


A shared library/ Dynamic Library is a library that is loaded dynamically at runtime for each application that requires it. Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory.

Let´s create a static library:

 gcc *.c -c -fpic Then archive the library using ‘ar rcs liball.a *.0

Your program should include a prototype for each of the functions that exist in your library. If you’re using a header file for these prototypes, make sure to include the name of that file in your other files by using #include “<header file>

Program compilation
gcc -L. -lall -o my_program main.c

When compiling program files, we have to tell the compiler to use the library files and where to find them. ‘-l’ tells it we want to include library files. And ‘all’ tells it to look for the library liball.so. It’s important to leave the ‘lib’ and ‘.so’ out of the flag because the compiler already identifies library files that way. ‘-L.’ tells the compiler it can find the library file in the current directory.

Let´s create a dynamic library:

  1. gcc *.c -c -fpic
    The .c source files need to be prepared for use in a dynamic library. Since multiple programs can all use one instance of a dynamic library, the library can’t store data at fixed addresses. This is because the location of the library in memory will vary between programs. This is done by using the compiler flag -fpic. Since we need to apply this step after the compile process has generated the object code, the compiler must be told to halt and return one object file (.o) for each source file. This is done by using the -c flag 
  2. gcc *0 -shared -o liball.so                                                                                        The object files are now ready to be compiled into a dynamic library. This is done by compiling all of the .o files using by using the -shared flag. Later when compiling program files, the compiler identifies a library by looking for files beginning with ‘lib’ and ending with a library extension (.so for dynamic, .a for static). Therefore it’s important to name a library accordingly.
  3. export D_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
    Because a program needs to know where to look for library files, we must add that location to the environmental variable LD_LIBRARY_PATH.


Differences between static library and dynamic library



Advantages and drawbacks

The drawback of using a static library is that it’s code is locked into the final executable file and cannot be modified without a re-compile. In contrast, a dynamic library can be modified without a need to re-compile.

The drawback of using a dynamic library is that a program is much more susceptible to breaking. If a dynamic library for example becomes corrupt, the executable file may no longer work. A static library, however, is untouchable because it lives inside the executable file.


The advantages of using a dynamic libraries:

1.  multiple running applications can use the same library without the need for each to have it’s own copy.

2. Execution speed at run-time. Because the it’s object code (binary) is already included in the executable file, multiple calls to functions can be handled much more quickly than a dynamic library’s code, which needs to be called from files outside of the executable.






Comentarios

Entradas populares de este blog

C static libraries

How Do SQL Database Engines Work?