Linking in Linux
Linux like Windows also supports all the linking of Windows. We shall discuss all the topics one by one briefly.
Static Linking in Linux
Static linking is the process of linking an object code directly to the executable code of the application during linking or building time of the executable.
Static linking is a compilation / build time process.
Let us consider mathlib.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.
Linux library project
Linux library source
Linux library header
Linux static library
$cc -fPIC -c mathlib.c
Now make a archive or static lib with the object file.
$ar rc libmath.a mathlib.o
Linux library application
To use this static library in a application we need to do the following steps:
- compile the application code (place math.h in include folder)
$cc -c libapp.c -Iinclude -o app.o
- Link with static library math.a
$ld app.o libmath.a -o app
- Run the application
$./app
Dynamic Linking
Dynamic linking is the process of linking a library function at the time of loading the application or during the runtime of the application. In contrast to static linking this happens during execution. Dynamic linking is divided into two categories - "Implicit dynamic linking" and "Explicit dynamic linking".
Implicit Dynamic Linking
Implicit dynamic linking is the process of linking a function from a shared library during the loading of the application.
Let us consider once again mathlib.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 mathlib.c
Now make a shared library with the object file.
$cc -shared libmath.so mathlib.o
To use this shared library in a application we need to do the following steps:
- compile the application code (place math.h in include folder)
$cc -c libapp.c -Iinclude -o app.o
- Link with import library math.lib
$ld app.o -lmath -o app
- Copy the libmath.so in lib path or current path and run the application
$./app
Explicit Dynamic Linking
Explicit dynamic linking is the process of linking a function from a shared library during the runtime of the application using verious helper function of the dynamic loader.
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 mathlib.c
Now make a shared library with the object file.
$cc -shared libmath.so mathlib.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/load library APIs to achieve this. Her are some useful frequently use APIs:
- dlopen() - loads a dynamic link binary
- dlsym() - returns the function pointer if found the function entry
- dlclose() - unloads the dynamic link binary
Linux Explicit library App project
Explicit SO, App build and run
About our authors: Team EQA
You have viewed 1 page out of 27. Your DLL learning is 0.00% complete. Login to check your learning progress.