After ten years of development, the Linux system has already caught up with Microsoft. As a result, many people start to learn Linux, and you may encounter explicit calls from dynamic libraries, here we will introduce the explicit call solution of the dynamic library. Here we will introduce it to you.
Explicit calling means that the library file name appears in the code, and you need to open and manage the library file on your own. The main points are:
(1) include the header file of the dlfcn. h System
(2) Use the dlopen function to open the library file and specify the open mode.
The first parameter of dllope is the name of the shared library, and the specified shared library will be searched in the following location.
① Environment variable LD_LIBRARY_PATH lists all directories separated by semicolons.
② The list of databases found in the file/etc/ld. so. cache is refreshed by the ldconfig command.
③ Directory usr/lib.
④ Directory/lib.
⑤ Current directory.
The second parameter is how to open the shared library. There are two values
① RTLD_NOW: load all functions in the shared library to the memory.
② RTLD_LAZY: pushes back the function loading operation in the shared library until a function is loaded when dlsym () is called.
(3) Use the dlerror () function to test whether the function is successfully opened and handle the error;
(4) use dlsym to obtain the function address and store it in a function pointer.
Then, use the obtained function pointer to call the function.
When the ghost program ends, use dlclose to close the opened dynamic library to prevent resource leakage.
⑺ Use the ldconfig tool to add the dynamic library path to the system library list.
1. Compile the test file
- // Main. c test the program explicitly called by the dynamic library
- # Include<Dlfcn. h>// System header file used for dynamic Library Management
- # Include "myalib. h" // include the function header file. Otherwise, an error is reported during compilation.
- Int main (int argc, char * argv [])
- {
- // Declare the function pointer of the corresponding function
- Void (* pTest )();
- // Load the dynamic library
- Void *PdlHandle=Dlopen("Libtest. so", RTLD_LAZY );
- // Handle errors
- If (PdlHandle= NULL ){
- Printf ("Failed load library \ n ");
- Return-1;
- }
- Char *PszErr=Dlerror();
- If (pszErr! = NULL)
- {
- Printf ("% s \ n", pszErr );
- Return-1;
- }
- // Obtain the function address
- PTest=Dlsym(PdlHandle, "test ");
- PszErr=Dlerror();
- If (pszErr! = NULL)
- {
- Printf ("% s \ n", pszErr );
- Dlclose (pdlHandle );
- Return-1;
- }
- // Implement function call
- (* PTest )();
- // Close the dynamic library when the program ends
- Dlclose (pdlHandle );
- Return 0;
- }
2. Compile the test file
Use the-ldl option to indicate that the generated object module must use the shared library.
Gcc-o main-ldl main. c
A main file is generated after execution.
3. Execute the test program
Run./main
Output
Test
It indicates the operation is successful. This is an introduction to explicit calling of Linux dynamic libraries.
- Configuration notes: configure the DNS server in Linux
- How to display Chinese Characters in RedHat Linux 5
- How to disable SELinux in Redhat Enterprise Linux
- Install the KDE package in Linux
- Redhat Linux Remote Desktop configuration