Use of dynamic link libraries under Linux-dlopen dlsym dlclose dlerror (RPM)

Source: Internet
Author: User
Tags function definition function prototype

Dlopen

Basic definition

Function: Open a dynamic link library  
contains header file:  
#include <dlfcn.h> 
function definition:  
void * Dlopen (const C Har * pathname, int mode);  
function Description:  
Opens the specified dynamic connection library file in the specified mode in the Dlopen () function, and returns a handle to the calling process. Use Dlclose () to unload the open library.  
Mode: Divided into these two kinds of  
Rtld_lazy suspend the decision, and so on when necessary to solve the symbol  
Rtld_now immediately decides to dismiss all pending symbols before returning.  
rtld_local 
Rtld_global allow export of symbols  
rtld_group 
rtld_world 
return value: &NBSP;
open error returned null 
successful, return library reference  
compile time to add-LDL (Specify DL library)  
For example  
gcc test.c-o test-ldl
Edit this paragraph
Using Dlopen
Dlopen () is a powerful library function. The function opens a new library and loads it into memory. This function is primarily used to load symbols in the library, which are not known at compile time. The Apache Web server, for example, uses this function to load modules during operation, which provides additional capabilities. A configuration file controls the process of loading the module. This mechanism makes it unnecessary to recompile a module when it is added to or removed from the system.  
can use Dlopen () in its own program. Dlopen () is defined in the Dlfcn.h and implemented in the DL library. It requires two parameters: a file name and a flag. The file name can be the soname in the library we have studied. Flag indicates whether the library's dependencies are calculated immediately. If set to Rtld_now, it is calculated immediately, if Rtld_lazy is set, it is calculated when needed. In addition, you can specify Rtld_global, which makes the symbols available for libraries that are loaded later.  

When the library is loaded, the handle returned by Dlopen () can be used as the first parameter to Dlsym () to obtain the address of the symbol in the library. With this address, you can get a pointer to a specific function in the library and call the corresponding function in the load library.

--------------------------------------------------------------------------------------------------------------- -----------

Dlsym

The function prototype of Dlsym () is
void* dlsym (void* handle,const char* symbol)
The function is in the <dlfcn.h> file.
Handle is the pointer that is returned by Dlopen when the dynamic-link library is opened, and symbol is the name of the function that is required, and the function return value is void*, which points to the address of the function for the call to use

To take the dynamic object address:
#include <dlfcn.h>
void *dlsym (void *phandle, char *symbol);
Dlsym returns the address of the symbol according to the dynamic Link library operation handle (Phandle) and symbol.
You can use this function not only to get the address of the function, but also to get the variable address. For example, suppose that in so
Defines a void mytest () function, which declares a function pointer when using so:
void (*pmytest) (), and then uses the Dlsym function to point the function pointer pmytest to the MyTest function,
Pmytest = (void (*) ()) Dlsym (Phandle, "mytest");

--------------------------------------------------------------------------------------------------------------- -----------

Dlclose
Dlclose ()
Include Header files:
#include <dlfcn.h>
The function prototypes are:
int dlclose (void *handle);
Function Description:
Dlclose is used to close the dynamic-link library of the specified handle, and only if the use count of this dynamic-link library is 0 o'clock is it actually uninstalled by the system.

--------------------------------------------------------------------------------------------------------------- -----------

Dlerror
Dlerror ()
Include Header files:
#include <dlfcn.h>
Function Prototypes:
const char *dlerror (void);
Function Description:
When the dynamic link library operation function fails, Dlerror can return an error message, and a return value of NULL indicates that the operation function executed successfully.

Linux creation and use of dynamic link libraries is not a difficult task.
The-shared option is used when compiling a function source to create a dynamic-link library, which should be named after the. so suffix, preferably under a common library directory (such as/lib,/usr/lib, etc.), and to write the user interface file so that other users can share it.
Use the dynamic link library, the source program to include the Dlfcn.h header file, write the program attention Dlopen functions such as the correct call, compile with the-rdynamic option and-LDL option, to produce a call to the dynamic link library execution code.

exampleload the math library, and print the cosine of2.0: #include<stdio.h>#include<dlfcn.h>intMainintargcChar**argv) {    void*handle; Double(*cosine) (Double); Char*error; Handle= Dlopen ("libm.so", Rtld_lazy); if(!handle) {fprintf (stderr,"%s\n", Dlerror ()); Exit (1); } cosine= Dlsym (Handle,"Cos"); if(Error = Dlerror ())! =NULL) {fprintf (stderr,"%s\n", error); Exit (1); } printf ("%f\n", (*cosine) (2.0));    Dlclose (handle); return 0;} If ThisProgram wereinchA file named"foo.c", you would build the program with the following COMMAND:GCC-rdynamic-o Foo FOO.C-LDL

Use of dynamic link libraries under Linux-dlopen dlsym dlclose dlerror (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.