Static & Dynamic Libraries

Static and Dynamic Libraries


Static and Dynamic libraries are two processes of collecting and combining multiple object files in order to create a single executable. The compiler generates object code when compiled. After generating object code, the compiler also invokes Linker. One of the main tasks of Linker is to make code of library functions (printf(), scanf()) available to your program.

Linking can accomplish the tasks in two ways.
  • By copying the code of library function to the object code.
  • By making some arrangements so that the complete code of library function is not copied, but made available at run-time.


Static Linking:

During Static linking the linker copies all library routines used in the program into the executable image/ binary. This ofcourse takes more space on the disk and in memory than dynamic linking. But Static linking is faster and more portable because it does not require the presence of the library on the system where it runs.

This is performed by the linker and it is done as the last step of compilation process.

Let’s see static linking by example, Add to integer numbers to demonstrate the static linking process. We will develop a sum module and place it in a separate sum.c file. Function signature of sum module will be placed in separate sum.h file. Code file Math.c will be created to demonstrate the linking process.

To begin with, create a header file sum.h and insert the sum function signature into that as follows.

Int sum(int, int);

Now, create another source code Math.c, and insert the following code

#include<sum.h>
#include<stdio.h>
#include<iostream>

Int main()
{
            Int x = 10, y = 20;
            Std::cout<<x<<” + ”<<y<<” = ”<<sum(s,y)<<endl;
            Return 0;          
}

Create one more file sum.c that contains the code of sum module.

Sum.c

Int sum(int a, int b)
{
            Return a+b;
}


gcc –I . –c Math.c

  • -Ioption tells gcc to search for header files in the current directory
  • –c option tells gcc to compile to an object file.

  
gcc -c sum.c

It will generate sum.o object file

Create the lbrary libMath.a

ar rs libMath.a sum.o


Execute the below command to generate the final executable file

gcc –o Math –L . Math.o -lMath


The –L option tells the linker to search for libraries in the specified directory.

Dynamic Linking:


In dynamic linking shareable library name is placed in the executable image/binary, while actual linking takes place at run time when both the executable and the library are placed in memory. Dynamic linking serves the advantage of sharing a single shareable library among multiple programs.

Dynamic linking refers much of the linking process until a program starts running. It performs the linking process “on the fly” as programs are executed in the system. During dynamic linking the name of the shared library is placed in the final executable file while the actual linking takes place at run time when both executable file and library are placed in the memory.


The –fPIC option enable “position independent code” generation.

gcc –Wall –fPIC –c sum.c


gcc –shared –o libMath.so sum.o

But to use shared library – Use below command

gcc –o Math Math.o –lMath


One can list the shared library dependencies which your executable is dependent upon. The ldd <executable-name> command does that for you..

Comments

Popular posts from this blog

C++ Guidelines for Multithreaded System

Signalling System #7(SS7) Protocol.

std::shared_ptr