当前位置: 技术问答>linux和unix
dlsym函数问题
来源: 互联网 发布时间:2017-02-03
本文导语: dlsym函数是void型,但是我用它提取的函数是short型的,例: short SingleTagIdentify=dlsym(handle,"SingleTagIdentify"); 这种写法是错误的,所以请问这个需要怎么转换啊?请各位高手指点! | 首先,你...
dlsym函数是void型,但是我用它提取的函数是short型的,例:
short SingleTagIdentify=dlsym(handle,"SingleTagIdentify");
这种写法是错误的,所以请问这个需要怎么转换啊?请各位高手指点!
short SingleTagIdentify=dlsym(handle,"SingleTagIdentify");
这种写法是错误的,所以请问这个需要怎么转换啊?请各位高手指点!
|
首先,你要知道dlsym的函数原型,这样才能保证类型匹配:
void* dlsym( void* handle, const char* name );
Examples:
Use dlsym() to find a function pointer and a pointer to a global variable in a shared library:
typedef int (*foofunc)( int );
void* handle;
int* some_global_int;
foofunc brain;
/* Open a shared library. */
handle = dlopen( "/usr/nto/x86/lib/libfoo.so.1", RTLD_NOW );
/* Find the address of a function and a global integer. */
brain = (foofunc)dlsym( handle, "takeover_world" );
some_global_int = (int* )dlsym( handle, "my_global_int" );
/* Invoke the function and print the int. */
x = (*brain)( 5 );
printf( "that global is %dn", *some_global_int );Check to see if a function is defined, and call it if it is:
typedef int (*funcptr)( void );
funcptr funk = NULL;
funk = (funcptr)dlsym( RTLD_DEFAULT, "get_funky" );
if( funk != NULL ) {
(*funk)();
}
void* dlsym( void* handle, const char* name );
Examples:
Use dlsym() to find a function pointer and a pointer to a global variable in a shared library:
typedef int (*foofunc)( int );
void* handle;
int* some_global_int;
foofunc brain;
/* Open a shared library. */
handle = dlopen( "/usr/nto/x86/lib/libfoo.so.1", RTLD_NOW );
/* Find the address of a function and a global integer. */
brain = (foofunc)dlsym( handle, "takeover_world" );
some_global_int = (int* )dlsym( handle, "my_global_int" );
/* Invoke the function and print the int. */
x = (*brain)( 5 );
printf( "that global is %dn", *some_global_int );Check to see if a function is defined, and call it if it is:
typedef int (*funcptr)( void );
funcptr funk = NULL;
funk = (funcptr)dlsym( RTLD_DEFAULT, "get_funky" );
if( funk != NULL ) {
(*funk)();
}
|
编译时链接dl库,也就是加上选项: -ldl