1) Library is a set of functions which are used by another executable files.
ii) Contents of this library are attach with the executable file permanently due to which a size of executable file get increase permanently.
iii) In case of static library same library can be loaded multiple times in memory due to which there is an memory wastage.
iv) The extension of static link library is generally ".a" .
--------------------------------------------------------------------------------------------------------------------------
Step 5:- Convert this .o file into .a as a library by calling "ar" command which is use to create archieve.
"ar rcs libdemo.a sharedfile.o"
After this command "libdemo.a" file gets created which contains all the function of our library file in executable format.
In this command "ar" is the name of command and "rcs" is the option which is use to create relocatable compile segment. "libdemo.a" is the name of library file.
In linux, name of library file should be start with "lib" word and extension should be ".a".
--------------------------------------------------------------------------------------------------------------------------
Step 6:- Now we have to use the command which is use to link "libdemo.a" file with "main.c" file.
" gcc -static main.c -L. -ldemo -o main"
i) gcc :- Command for linking.
ii) -static :- Link option.
ii) main.c :- Input file which use our library.
iv) -L ;- Location mask which specify the location of library.
v) . :- Location of library ( Indicate current location).
vi) main :- Name of executable file as output.
After using this command we get output file as executable file named as "main".
--------------------------------------------------------------------------------------------------------------------------
Step 7:- We can execute that file using command as,
" ./main"
--------------------------------------------------------------------------------------------------------------------------
We can display the contents of library i.e. static library or shared library as :-
" nm -g libdemo.a "
-------------------------------------------------------------------------------------------------------------------------
To display the size of our executable file by using the command as :-
" size ./main ".
-------------------------------------------------------------------------------------------------------------------------
2. Shared object or dynamic link library in Linux :-
1) Concept of shared object is same as dynamic link library in windows.
2) Extension of shared object file as ".so".
3) It is called as dynamic linking because library gets loaded in memory when executable process demands for that library.
4) If our process want to use the functionality of shared object then we have to call some function which are use to load that library in memory, get the address of desire function and unload that library.
The major advantage of shared library is that the actual source code gets loaded in memory only once.
5) The same source code should be used by different processes simultaneously.
6) Operating system is responsible to maintain the record of library files which are loaded in memory.
--------------------------------------------------------------------------------------------------------------------------
Steps to create shared object:-
Step 1 :- Create .c as shared file, which contains the functionality which has to be shared by other application.
// sharedfile.c
#include "sharedfile.h"
void DemoFun()
{
printf("Inside shared file");
}
--------------------------------------------------------------------------------------------------------------------------
Step 2 :- Create header file which contains the prototype of functions as
// sharedfile.h
#include<stdio.h>
extern void DemoFun();
--------------------------------------------------------------------------------------------------------------------------
Step 3 :- Create other .c file which internally uses the functionality of ".so" file.
// main.c
#include<stdio.h>
#include<stdlib.h>
#include<dlfcn.h>
// This header file is use for shared object functions such as dlopen(), dlsym(), dlclose() etc.
int main()
{
void *p = NULL;
void (*fp)() = NULL; // This function pointer use to hold the address of library function.
p = dlopen("path of .so file", RTLD_LAZY);
fp = dlsym( p, "DemoFun" );
fp(); // call library function.
dlclose(p);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------
Explaination :-
dlopen() :- This function is use to load our library into memory.
dlsym() :- This function is use to get the address of desired function from .so file.
dlclose() :- It is use to detach .so file from memory.
-------------------------------------------------------------------------------------------------------------------------
Step 4 :- Convert sharedfile.c file into sharedfile.o i.e. object file.
" gcc -fPIC sharedfile.c " or "gcc -c sharedfile.c -o sharedfile.o"
After this command sharedfile.o file is created.
-------------------------------------------------------------------------------------------------------------------------
Step 5 :- Now convert this sharedfile.o file into our .so file.
" gcc -shared -o library.so sharedfile.o "
After this command library.so file gets created.
-------------------------------------------------------------------------------------------------------------------------
Step 6 :- Now create executable which uses .so file as
" gcc -rdynamic -o main main.c -ldl "
After this main execuable gets created.
--------------------------------------------------------------------------------------------------------------------------
Step 7 :- We can execute that file as
" ./main "
--------------------------------------------------------------------------------------------------------------------------
thank you,
can you regarding any query please ask me very frankly.
Mob. No. :- +91 9767623508
Email ID :- sunilbhagwat24@gmail.com
--------------------------------------------------------------------------------------------------------------------------
2) Library are consider as dependable executable, which is not directly run because we have to need some executable files for linking purpose.
3) According to the linking there are two types of library:-
i) Static link library.
ii) Dynamic link library.
--------------------------------------------------------------------------------------------------------------------------
1) Static Link Library in Linux:-
i) This type of library gets link to executable at compile time.
ii) Contents of this library are attach with the executable file permanently due to which a size of executable file get increase permanently.
iii) In case of static library same library can be loaded multiple times in memory due to which there is an memory wastage.
iv) The extension of static link library is generally ".a" .
-------------------------------------------------------------------------------------------------------------------------
/* Program to use static link library */
Step 1:- Create header file which contains prototypes of functions from our library.
// sharedfile.h
--------------------------------------------------------------------------------------------------------------------------
int DemoAdd(int, int);
int DemoMultiply(int, int);
--------------------------------------------------------------------------------------------------------------------------
Step 2:- Create normal .c file which contains the function defination for our library.
// sharedfile.c
--------------------------------------------------------------------------------------------------------------------------
int DemoAdd(int iNum1, int iNum2)
{
return iNum1 + iNum2;
}
int DemoMultiply(int iNum1, int iNum2)
{
return iNum1 * iNum2;
}
--------------------------------------------------------------------------------------------------------------------------
Step 3:- Create other .c file which internally calls the function of our library file.
//main.c
--------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include "sharedfile.h"
int main()
{
int iAns = 0, iAns1 = 0;
iAns = DemoAdd(10, 20);
printf("The Addition of 10 and 20 is %d \n", iAns);
iAns1 = DemoMultiply(10, 20);
printf("The Multiplication of 10 and 20 is %d \n", iAns1);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------
Step 4:- Compile our library file to convert it into .o file.
"gcc -c sharedfile.c -o sharedfile.o"
after this command .o file gets created which contains object code of our library file.
--------------------------------------------------------------------------------------------------------------------------
Step 5:- Convert this .o file into .a as a library by calling "ar" command which is use to create archieve.
"ar rcs libdemo.a sharedfile.o"
After this command "libdemo.a" file gets created which contains all the function of our library file in executable format.
In this command "ar" is the name of command and "rcs" is the option which is use to create relocatable compile segment. "libdemo.a" is the name of library file.
In linux, name of library file should be start with "lib" word and extension should be ".a".
--------------------------------------------------------------------------------------------------------------------------
Step 6:- Now we have to use the command which is use to link "libdemo.a" file with "main.c" file.
" gcc -static main.c -L. -ldemo -o main"
i) gcc :- Command for linking.
ii) -static :- Link option.
ii) main.c :- Input file which use our library.
iv) -L ;- Location mask which specify the location of library.
v) . :- Location of library ( Indicate current location).
vi) main :- Name of executable file as output.
After using this command we get output file as executable file named as "main".
--------------------------------------------------------------------------------------------------------------------------
Step 7:- We can execute that file using command as,
" ./main"
--------------------------------------------------------------------------------------------------------------------------
We can display the contents of library i.e. static library or shared library as :-
" nm -g libdemo.a "
-------------------------------------------------------------------------------------------------------------------------
To display the size of our executable file by using the command as :-
" size ./main ".
-------------------------------------------------------------------------------------------------------------------------
2. Shared object or dynamic link library in Linux :-
1) Concept of shared object is same as dynamic link library in windows.
2) Extension of shared object file as ".so".
3) It is called as dynamic linking because library gets loaded in memory when executable process demands for that library.
4) If our process want to use the functionality of shared object then we have to call some function which are use to load that library in memory, get the address of desire function and unload that library.
The major advantage of shared library is that the actual source code gets loaded in memory only once.
5) The same source code should be used by different processes simultaneously.
6) Operating system is responsible to maintain the record of library files which are loaded in memory.
--------------------------------------------------------------------------------------------------------------------------
Steps to create shared object:-
Step 1 :- Create .c as shared file, which contains the functionality which has to be shared by other application.
// sharedfile.c
#include "sharedfile.h"
void DemoFun()
{
printf("Inside shared file");
}
--------------------------------------------------------------------------------------------------------------------------
Step 2 :- Create header file which contains the prototype of functions as
// sharedfile.h
#include<stdio.h>
extern void DemoFun();
--------------------------------------------------------------------------------------------------------------------------
Step 3 :- Create other .c file which internally uses the functionality of ".so" file.
// main.c
#include<stdio.h>
#include<stdlib.h>
#include<dlfcn.h>
// This header file is use for shared object functions such as dlopen(), dlsym(), dlclose() etc.
int main()
{
void *p = NULL;
void (*fp)() = NULL; // This function pointer use to hold the address of library function.
p = dlopen("path of .so file", RTLD_LAZY);
fp = dlsym( p, "DemoFun" );
fp(); // call library function.
dlclose(p);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------
Explaination :-
dlopen() :- This function is use to load our library into memory.
dlsym() :- This function is use to get the address of desired function from .so file.
dlclose() :- It is use to detach .so file from memory.
-------------------------------------------------------------------------------------------------------------------------
Step 4 :- Convert sharedfile.c file into sharedfile.o i.e. object file.
" gcc -fPIC sharedfile.c " or "gcc -c sharedfile.c -o sharedfile.o"
After this command sharedfile.o file is created.
-------------------------------------------------------------------------------------------------------------------------
Step 5 :- Now convert this sharedfile.o file into our .so file.
" gcc -shared -o library.so sharedfile.o "
After this command library.so file gets created.
-------------------------------------------------------------------------------------------------------------------------
Step 6 :- Now create executable which uses .so file as
" gcc -rdynamic -o main main.c -ldl "
After this main execuable gets created.
--------------------------------------------------------------------------------------------------------------------------
Step 7 :- We can execute that file as
" ./main "
--------------------------------------------------------------------------------------------------------------------------
thank you,
can you regarding any query please ask me very frankly.
Mob. No. :- +91 9767623508
Email ID :- sunilbhagwat24@gmail.com
--------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment