日期:2014-05-16  浏览次数:20752 次

dlsym函数问题
dlsym函数是void型,但是我用它提取的函数是short型的,例:
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 %d\n", *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)();
}
------解决方案--------------------
探讨

非常感谢各位的帮助,在各位高手的指点下,编译终于通过了,真的是类型转换的问题!
但是最后链接的时候出现了错误:
(.text+0x68): undefined reference to `dlopen'
(.text+0xa2): undefined reference to `dlsym'
collect2: ld returned 1 exit status
烦请各位再帮忙看看好吗……