/*creating reqired Files*/ vim add.h .... #ifndef add_h_ #define add_h_ extern int add(int , int ); #endif ...
vim add.c /*Definition of Shared Library*/ #include<stdio.h> int add(int a,int b) { puts("Hello,I am a shared library"); return (a+b); } .... vim main.c /*Executable code to run to call a shared Library Function*/ #include<stdio.h> #include"add.h" int main() { int sum; int a,b; puts("Welcome to a shared library test..."); printf("enter a and b \n"); scanf("%d",&a); printf("entered a = %d \n",a); scanf("%d",&b); printf("entered b = %d \n",b); sum = add(a,b); printf("\nsum of a & b is = %d\n",sum); return 0; } COMMANDS USED : 1) gcc -c -Wall -Werror -fpic add.c // to display all types of errors while compiling.
2)gcc -shared -o libadd.so add.o // Compiling and creating libadd.so File with lib and .so as presfix and suffix to repesent it is shared library
/* YOU WILL FIND A libadd.so file in the same directory Which is an ELF - executable and linkable file*/
/*compiling library add and creating an ELF as test file to run your code 3)gcc -L/home/jarvis/shrdlib -Wall -o test main.c -ladd ls ./test echo $LD_LIBRARY_PATH LD_LIBRARY_PATHi /home/jarvis/shrdlib 4)LD_LIBRARY_PATH=/home/jarvis/shrdlib:$LD_LIBRARY_PATH // Loading in to kernel of the current path into loader
5) export LD_LIBRARY_PATH=/home/jarvis/shrdlib:$LD_LIBRARY_PATH //exporting library so that linker finds it in default paths in the kernel//