Because the C compiler is broken. There is no conversion (explicit or implicit) between void* and the function pointer, neither in C nor in C ++.
Posix adds a restriction on C and requires that void* and function pointers have the same size and presentation, so that:
void (*myFunc)( char * ); *(void (**myFunc)( char* ))( &myFunc ) = dlsym(...);
will work.
In C ++, you can use something like:
class GetFunctionHelper; GetFunctionHelper getFunction( void* dlHandle, std::string const& functionName ); class GetFunctionHelper { void* fromSystem; freind GetFunctionHelper getFunction( void* , std::string const& ); GetFunctionHelper( void* fromSystem ) : fromSystem( fromSystem ) {} public: template <typename Ptr> operator Ptr() const { return *reinterpret_cast<Ptr const*>( &fromSystem ); } }; GetFunctionHelper getFunction( void* dlHandle, std::string const& functionName ) { return GetFunctionHelper( dlsym( dlHandle, functionName.c_str() ) ); }
(with a bit more error checking, of course).
source share