The "dlsym" function returns "void *". The "Examples" section recommends
casting the return value to a pointer to a function when using "dlsym" to find
the address of a function. Unfortunately, casting any data pointer (including
"void *") to a function pointer violates the ISO C Standard and causes warnings
on some compilers.
It is legal to convert from a pointer to a function type into a pointer to a
function of another type, and then convert the pointer back to the original
type. Therefore, I would like to see a function added that instead returns a
"void (*)(void)". Something like:
void (*)(void) dlfunc(void *restrict handle, const char *restrict name);
This return type could then be converted into the actual function type of the
function. So the example given in the Specification would be changed to:
void *handle;
int *iptr, (*fptr)(int);
/* open the needed object */
handle = dlopen("/usr/home/me/libfoo.so", RTLD_LOCAL | RTLD_LAZY);
/* find the address of function and data objects */
fptr = (int (*)(int))dlfunc(handle, "my_function");
iptr = (int *)dlsym(handle, "my_object");
/* invoke function, passing value of integer as a parameter */
(*fptr)(*iptr);
Any suggestions for improving this idea?
=====
Sincerely,
Matt Seitz
__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/
|