Explicit Linking in C
Explicit linking is the process of linking a dynamic link library at run time explicitly by user. There are two types of linking - static and dynamic. Static linking is build time linking. Code of the static library adds to the binary during compilation/linking time. Now dynamic linking is the process where application is compiled without library binary. The library is loaded during load time. Implicit linking is the process where OS loader loads the dependent library. Explicit Linking is the process where user loads the library using loader APIs and fetches the symbol load address and calls explicitly.
Windows Explicit Linking
When application does not links the external symbol by import library rather it loads the DLL at runtime. It does the same mechanism as operating system does during loading of the implicit linked DLL calls.
This is done by using Win32 APIs like :
- LoadLibrary() - loads and links a input library form the DLL path, or current path,
- GetProcAddress() - finds the symbol/function address by its name for a loaded DLL
- FreeLibrary() - unloads the loaded DLL instance
Windows DLL project
Windows App project
Linux Explicit 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 mathlib.c -o mathlib.o
Now make a shared library with the object file.
$cc -shared -o 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 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 SO project
Linux App project
SO and App build and run
About our authors: Team EQA
You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.