| 1. |
How We Use Dynamic Linking In Linux? Give Example? |
|
Answer» Linux like Windows also supports all the linking of Windows. We shall discuss all the topics one by one briefly. Static Linking: Let us consider math.c file. We want to make it as a static LIBRARY. First we compile it with position independent FLAG on(-fPIC). This is needed for dynamic/static linking. $cc -fPIC -c math.c Now make a archive or static lib with the object file. $ar rc libmath.a math.o To use this static library in a application we need to do the following steps:
Implicit Dynamic Linking: Let us consider once again math.c file for dynamic linking. We want to make it as a dynamic library. First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking. $cc -fPIC -c math.c Now make a shared library with the object file. $cc -shared libmath.so math.o To use this shared library in a application we need to do the following steps:
Explicit Dynamic Linking: Let us consider once again math.c file for explicit linking. The steps for creating a shared library are same as that of implicit linking. First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking. $cc -fPIC -c math.c Now make a shared library with the object file. $cc -shared libmath.so math.o To use this shared library in a application we need to load the library then find the function pointer address, invoke the function, and at last unload the library. Linux provides some dynamic link library APIs to achieve this. Her are some useful frequently use APIs:
SAMPLE code: #include Linux like Windows also supports all the linking of Windows. We shall discuss all the topics one by one briefly. Static Linking: Let us consider math.c file. We want to make it as a static library. First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking. $cc -fPIC -c math.c Now make a archive or static lib with the object file. $ar rc libmath.a math.o To use this static library in a application we need to do the following steps: Implicit Dynamic Linking: Let us consider once again math.c file for dynamic linking. We want to make it as a dynamic library. First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking. $cc -fPIC -c math.c Now make a shared library with the object file. $cc -shared libmath.so math.o To use this shared library in a application we need to do the following steps: Explicit Dynamic Linking: Let us consider once again math.c file for explicit linking. The steps for creating a shared library are same as that of implicit linking. First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking. $cc -fPIC -c math.c Now make a shared library with the object file. $cc -shared libmath.so math.o To use this shared library in a application we need to load the library then find the function pointer address, invoke the function, and at last unload the library. Linux provides some dynamic link library APIs to achieve this. Her are some useful frequently use APIs: Sample code: #include |
|