c preprocessor - How do I resolve this warning about passing a function pointer to a macro in C? -


i have macro defined as:

#define _call(func_name, __func_proto, ...)        {       \  void *_handle;                                           \  int result = !cerr_v_success;                               \                                                               \   _handle = dlopen(_dll_name,global); \   if (_handle) {                                           \       __func_proto = dlsym(dll_handle, func_name);            \       if (__func_proto) {                                     \           result = (*__func_proto)(__va_args__);              \      }                                                       \       (void)dlclose(_handle);                              \   }                                                           \   return (result);                                            \ } 

i calling macro different function pointers this:

_call("download",_download,src,dst); _call("upload",_upload,src,dst); _call("test",_test,file); 

and function pointers defined as

int (*_download )(int *,int *); int (*_upload)(int *,int*); int (*_test)(int *,char*); 

this results in warning a value of type "void *" cannot assigned entity of type "int (*)(int*,int*)".

how can resolve warning?

documentation of dlsym suggests should cast function pointer void* before assign return value pointer:

*(void **)(&__func_proto) = dlsym(dll_handle, func_name); 

rationale described behind link:

the iso c standard not require pointers functions can cast , forth pointers data. indeed, iso c standard not require object of type void * can hold pointer function. implementations supporting xsi extension, however, require object of type void * can hold pointer function.

note compilers conforming iso c standard required generate warning if conversion void * pointer function pointer attempted in:

fptr = (int (*)(int))dlsym(handle, "my_function");


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

android - CoordinatorLayout, FAB and container layout conflict -