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
Post a Comment