To create and use program Libraries in Linux > C Program

To create and use program Libraries in Linux > C Program

System Programming and Compiler Construction


main.c

#include<stdio.h>
#include<math.h>
#include "shared.h"
int main()
{
double result=5.0;
result=factorial(result);
printf("\n The factorial is [%g] \n",result);
return 0;
}



shared.c
#include<stdio.h>
#include<math.h>
#include "shared.h"
double factorial(double a)
{
if(a==0)
 return 1;
else
 return(a*factorial(a-1));
}



shared.h
#include<stdio.h>
#include<math.h>
extern double factorial(double a);



output:
[root@www ~]# cd Desktop
[root@www Desktop]# gcc -c -Wall -Werror -fpic shared.c
[root@www Desktop]# gcc -shared -o libshared.so shared.o
[root@www Desktop]# gcc -L /root/Desktop -Wall main.c -o main -lshared -lm
[root@www Desktop]# export LD_LIBRARY_PATH=/root/Desktop:$LD_LIBRARY_PATH
[root@www Desktop]# ./main
 The factorial is [120] 


Comments

Popular posts from this blog

Intermediate Code Generation > C Program