Process of Creating a Dynamic Library(.so) in C

Steps for creating a Dynamic Library in C

Step1: Implement source code for the library

add.c:We have implemented the addition function in the file

int add(int a, int b)
{
     return a+b;
}

sub.c :Implemented the subtraction function in the file

int sub(int a, int b)
{
     return a-b;
}

Step2: Compile the sources into position independent relocatable
 gcc -c -fpic add.c   O/P: add.o
 gcc -c -fpic sub.c   O/P: sub.o

Step3: As it has to be loaded into memory, we have to make it into ABI format which can be done by linker.So we will use dynamic linker and create shared object image.

     gcc --shared -o libarith.so add.o sub.o

Whenever we do file libarith.so ,it shows ELF it means that it can be loaded in the memory whereas the same is not true for static library image.

app.c:

#include <stdio.h>

int main()
{
   printf("add:%d\t sub:%d\n".add(2,3),sub(2,3));
   return 0;
}

Compile it with the following command:
gcc app.c -o app ./libarith.so


Comments

Popular posts from this blog

bb.utils.contains yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig

PR, PN and PV Variable in Yocto