Professional Documents
Culture Documents
Instructor
Prof. Pranav Jha.
Submitted by
Narendra Sisodiya
(2006EET2473)
Linux :
Creating Static Libraries
we have 4 files with us
1.fun1.c, it store code of “add” and “sub” function.
2.fun2.c it store code of “mul” and “div” function.
3.header.h it store prototype of “add” , “sub” ,“mul” ,” div” function.
4.user_pro.c user program that calls the function.
We have some intermediate files
1.fun1.o object code for fun1.c
2.fun2.o object code for fun2.c
3.mylib_sta.a static library made by combining both object file
4.user_pro.o object code for user_pro.c
we have final executable file user_pro_final
1.user_pro_final
=====================================================
user_pro.c
------------------------------------------------------------------------------------------
#include"header.h"
#include<stdio.h>
main()
{
int a,b;
printf("Enter a: ");
scanf("%d",&a);
printf("Enter b: ");
scanf("%d",&b);
printf("\naddition of a and b is %d: ",add(a,b));
printf("\nsutraction of a and b is %d: ",sub(a,b));
printf("\nmultiplication of a and b is %d: ",mul(a,b));
printf("\ndivide of a and b is %f: ",div(a,b));
return 0;
}
================================================
$ pwd
/home/student/Narendra/static
step 2 : create static named mylib_sta.a library from fun1.o and fun2.o
$ ./user_pro_final
Enter a: 45
Enter b: 12
addition of a and b is 57:
subtraction of a and b is 33:
multiplication of a and b is 540:
dividing of a and b is 3.750000:
$ LD_LIBRARY_PATH=/home/student/Narendra/shared
$ export LD_LIBRARY_PATH
$ ./user_pro_final_exe
Enter a: 45
Enter b: 12
addition of a and b is 57:
subtraction of a and b is 33:
multiplication of a and b is 540:
dividing of a and b is 3.750000:
Lazy Linking
we have to modify the code in main program
====================================
user_pro.c
----------------------------------------------------------------
#include"header.h"
#include<stdio.h>
#include<dlfcn.h>
int main()
{
void* funptr;
int (*add)(int a,int b);
int (*sub)(int a,int b);
int (*mul)(int a,int b);
float (*div)(int a,int b);
if (!funptr)
{
fprintf(stderr, "Error during dlopen(): %s\n", dlerror());
return 1 ;
}
add= dlsym(funptr, "add");
sub= dlsym(funptr, "sub");
mul= dlsym(funptr, "mul");
div= dlsym(funptr, "div");
int a,b;
printf("Enter a: ");
scanf("%d",&a);
printf("Enter b: ");
scanf("%d",&b);
printf("addition of a and b is %d: ",add(a,b));
printf("\nsubtraction of a and b is %d: ",sub(a,b));
printf("\nmultiplication of a and b is %d: ",mul(a,b));
printf("\ndividing of a and b is %f: ",div(a,b));
printf("\n");
dlclose(funptr);
return 0;
}
====================================================
$ pwd
/home/student/Narendra/dyna
$ ./user_pro_final
Enter a: 45
Enter b: 12
addition of a and b is 57:
subtraction of a and b is 33:
multiplication of a and b is 540:
dividing of a and b is 3.750000:
========================================================
Windows Environment:
In windows we use VC++ to create the Dll. The following process have to be
followed to make the Dll.
==========================================================
dllfun1.cpp
---------------------------------------------------------------------------------------------------
// dllfun1.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "dllfun1.h"
==========================================================
• Open the dllfun1..h file and insert the declaration
===================================================
dllfun1.h
--------------------------------------------------------------------------------------
#ifdef DLLFUN1_EXPORTS
#define DLLFUN1_API __declspec(dllexport)
#else
#define DLLFUN1_API __declspec(dllimport)
#endif
===============================================
user_pro.cpp
---------------------------------------------------------------------------------------
#include <Windows.h>
#include <iostream.h>
void main()
{
HINSTANCE hDll;
hDll = LoadLibrary("C:/mydll/dllfun1.dll");
if(hDll == NULL)
{ cout<<"Error loading dllfun1.dll"<<endl;return ;
}
add = (FunPtrTypeInt)GetProcAddress(hDll,"add");
if(add == NULL)
{
cout<<"error:"<<GetLastError()<<endl;
}
sub = (FunPtrTypeInt)GetProcAddress(hDll,"sub");
if(sub == NULL)
{
cout<<"error:"<<GetLastError()<<endl;
}
hDll = LoadLibrary("C:/mydll/dllfun2.dll");
if(hDll == NULL)
{
cout<<"Error loading dllfun2.dll"<<endl;
return ;
}
mul = (FunPtrTypeInt)GetProcAddress(hDll,"mul");
if(mul == NULL)
{
cout<<"error:"<<GetLastError()<<endl;
}
div1 = (FunPtrTypeFloat)GetProcAddress(hDll,"div1");
if(div1 == NULL)
{
cout<<"error:"<<GetLastError()<<endl;
}
int a,b;
cout<<"Enter a:";cin>>a;
cout<<"Enter b:";cin>>b;
cout<<"a + b = "<<add(a,b)<<endl;
cout<<"a - b = "<<sub(a,b)<<endl;
cout<<"a * b = "<<mul(a,b)<<endl;
cout<<"a / b = "<<div1(a,b)<<endl;
}
====================================================
Enter a:
45
Enter b:
12
a + b = 53
a - b = 33
a * b = 540
a / b = 3.750000