How to load a dynamic link library in Linux: the following uses a touch screen as an example to describe how to load a shared library.
First, let's look at the following functions:
Dlopen ()
Function: open a dynamic link library.
Include header file:
# Include <dlfcn. h>
Function Definition:
Void * dlopen (const char * pathname, int mode );
Function Description:
In the dlopen () function, open the specified dynamic connection library file in the specified mode, and return a handle to the calling process. Use dlclose () to uninstall the opened library.
Dlclose (handle );
Function: Disable a dynamic link library.
Dlsym ()
Function prototype: void * dlsym (void * handle, const char * symbol)
This function is in the <dlfcn. h> file.
Handle is the pointer returned after dlopen opens the dynamic link library. symbol is the name of the function to be obtained. The return value of the function is void *. It points to the address of the function for calling.
Getenv ()
Function: obtains the environment variable value from the Environment string.
Header file: stdlib. h
Usage: char * getenv (char * envvar );
Function Description: getenv () is used to obtain the content of the enwar environment variable. The enwar parameter is the name of the environment variable. If the variable exists, a pointer to the content is returned. The environment variable format is enwar = value.
Returned value: if the execution is successful, a pointer to the content is returned. If no conforming environment variable name is found, null is returned.
Alloca ()
Contained in the header file malloc. H. In some systems, the macro is defined as _ alloca.
Int load_module (struct tsdev * ts, const char * module, const char * Params, int raw)
{
Struct tslib_module_info * (* init) (struct tsdev *, const char *);
Struct tslib_module_info * Info;
Char FN [1024];
Void * handle;
Int ret;
Char * plugin_directory = NULL;
If (plugin_directory = getenv ("tslib_plugindir "))! = NULL ){
// FN = alloca (sizeof (plugin_directory) + strlen (module) + 4 );
Strcpy (FN, plugin_directory );
} Else {
// FN = alloca (sizeof (plugin_dir) + strlen (module) + 4 );
Strcpy (FN, plugin_dir );
}
/* Construct the shared library path */
Strcat (FN ,"/");
Strcat (FN, module );
Strcat (FN, ". So ");
# Ifdef debug
Printf ("loading module % s \ n", FN );
# Endif
Handle = dlopen (FN, rtld_now );
If (! Handle)
Return-1;
Init = dlsym (handle, "mod_init ");
If (! Init ){
Dlclose (handle );
Return-1;
}
Info = Init (TS, Params );
If (! Info ){
Dlclose (handle );
Return-1;
}
Info-> handle = handle;
If (raw ){
Ret = _ ts_attach_raw (TS, Info );
} Else {
Ret = _ ts_attach (TS, Info );
}
If (RET ){
Info-> OPS-> Fini (Info );
Dlclose (handle );
}
Return ret;
}