Linux Dynamic Link
Applications in Linux can be linked to external functions in either of the following ways (lib*.a)Static LinkAnd include the library code in the executable file of the application; or (lib*.so)Dynamic Link. The dynamic link loader maps the dynamic library to the executable memory of the application. Before starting the application, the dynamic link loader maps the required shared target Library to the application memory, or uses the system shared destination to parse the required external references for the application. Now the application can run.
As an example, the following is a small program that demonstrates the default use of dynamic link libraries in Linux:
main(){ printf("Hello world");} |
When you use GCC to compile hello. C,a.out. Use Linux commandsldd a.out(This command prints the mutual dependency of the shared library.) It can be seen that the required shared library is:
libc.so.6 => /lib/libc.so.6 (0x4001d000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000) |
Use the same dynamic link loader to map the DLL to the application memory after the application program runs. By using the Linux dynamic loader routine, the application controls the dynamic library to be loaded and the function in the library to execute the loading and link and return the address of the required entry point.
Linux DLL Functions
Linux provides four library functions (dlopen,dlerror,dlsymAnddlclose), An include file (dlfcn.h) And two shared libraries (static databaseslibdl.aAnd dynamic librarylibdl.so) To support dynamic link loaders. These library functions are:
- DlopenOpen the shared target file, map it To the memory, and return a handle.
- DlsymReturns a pointer to the requested entry point.
- DlerrorReturns null or a pointer to the ASCII string that describes the most recent error.
- DlcloseClose the handle and cancel the ing of the shared target file
The dynamic link loader routine dlopen needs to find the shared target file in the file system to open the file and create a handle. There are four ways to specify the file location:
dlopen callAbsolute file path in
- In the directory specified in the LD_LIBRARY_PATH environment variable
- In the library List specified in/etc/lD. So. Cache
- First in/usr/lib, then in/lib
DLL example: small C program and dltest
The dynamic link loader sample program is a small C program designed to practice DL routines. This program is based on a C program compiled by everyone. It prints "Hello World" to the console. The first printed message is "Hello World ". The test program links to the two functions that print the message again: the first time, all uppercase characters, and the second time, all lowercase characters.
The following is a summary of the program:
- Use the absolute path "/home/dltest/lib library. So" and the option rtld_lazy,
dlopenOpen the DLL shared destination file and return the handle.
Option rtld_lazy delays parsing the DLL's external reference until the DLL is executed.
- Option rtld_now in
dlopenReturns all external references resolved previously.
dlsymReturns the function address of the entry point.
dlcloseClose the handle to Lib. So and cancel the DLL ing from the memory.
dlopenUse the relative path of the Environment Variable LD_LIBRARY_PATH to find the shared Destination path, open the DLL shared destination file lowercase. So, and return a handle.
Note: Each calldlopen,dlsymOrdlcloseThen, calldlerrorTo obtain the final error message and print the string of the error message.