I have the need to dynamically link against a library at run-time and resolve a series of functions using dlsym
. My first thought was to use an array of function pointers that can be easily iterated through by leveraging a secondary array of char *
representing the symbol names.
However, the problem with this is that not all of the functions take the same arguments. Is there a way to use a generic function pointer in the array, but assign it to a more restrictive function pointer prototype? For example:
If I need to resolve these functions:
int (*functionA)(int)
char (*functionB)(char,int)
Is it possible to do something like (pseudo-ish .. )
void* functionList(...)[2] = {functionA, functionB};
Along with
char FuncNameA[] = "functionA";
char FuncNameB[] = "functionB";
char *functionNames[2] = {FuncNameA, FuncNameB};
For the purpose of looping though the call to dlsym
for symbol resolution
int i = 0;
for(; i<2; i++)
functionList[i] = dlsym(DL_OPEN_HANDLE, functionNames[i]);
Where DL_OPEN_HANDLE would be defined by an earlier call to dlopen
.
void* (*func)(void*, ...)
, or similar. – Harmaningvoid*
is likely to work, but not guaranteed by the C standard. All object pointer types can be converted tovoid*
and back again without loss of information, but there's no such guarantee for converting function pointer types tovoid*
. – Salley