Originally Posted by
Tony Flury
The only other option is to store everything as a (void *)(void) , and make sure you cast to the right type when you try to call it.
TonyFlury, check this out. Is is okay ?
Basically, I am receiving the return from dlsym call as a void* and then typecasting it to type FPTR_REQ.(By REQ, I meant required)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
float fun(int, char*);
typedef void* (*FPTR_VOID)(void);
typedef float (*FPTR_REQ)(int,char*);
int main(void)
{
void* handle;
char* error;
FPTR_VOID fptr_void;
FPTR_REQ fptr_req;
float ret;
handle = dlopen(NULL,RTLD_LAZY);
if(!handle)
{
fprintf(stderr,"%s\n",dlerror());
exit(1);
}
dlerror();
fptr_void = dlsym(handle,"fun");
if((error = dlerror()) != NULL)
{
fprintf(stderr,"%s\n",error);
exit(1);
}
fptr_req = (FPTR_REQ)fptr_void;
ret = (*fptr_req)(1,"hello");
printf("ret == [%f]\n",ret);
return 0;
}
float fun(int a, char* str)
{
printf("a == [%d]\n",a);
printf("str == [%s]\n",str);
return 3.14;
}
Build steps :
gcc -rdynamic -o exe main.c -ldl
Bookmarks