I\'m trying to compile the following simple DL library example code from Program-Library-HOWTO with g++. This is just an example so I can learn how to use and write shared libr
C allows implicit casts from void *
to any pointer type (including function pointers); C++ requires explicit casting. As leiflundgren says, you need to cast the return value of dlsym()
to the function pointer type you need.
Many people find C's function pointer syntax awkward. One common pattern is to typedef the function pointer:
typedef double (*cosine_func_ptr)(double);
You can define your function pointer variable cosine
as a member of your type:
cosine_func_ptr cosine;
And cast using the type instead of the awkward function pointer syntax:
cosine = (cosine_func_ptr)dlsym(handle, "cos");