Tutorial :Using C++ Library in Linux (eclipse)



Question:

I wrote a library and want to test it. I wrote the following program, but I get an error message from eclipse.

#include <stdlib.h>  #include <stdio.h>  #include <dlfcn.h>    int main(int argc, char **argv) {      void *handle;      double (*desk)(char*);      char *error;        handle = dlopen ("/lib/CEDD_LIB.so.6", RTLD_LAZY);      if (!handle) {          fputs (dlerror(), stderr);          exit(1);      }        desk= dlsym(handle, "Apply"); // ERROR: cannot convert from 'void *' to 'double*(*)(char*)' for argument 1      if ((error = dlerror()) != NULL)  {          fputs(error, stderr);          exit(1);      }        dlclose(handle);  }  

What is wrong?

Regards.


Solution:1

In C++ you need the following:

  typedef double (*func)(char*);  func desk = reinterpret_cast<func>( dlsym(handle, "Apply"));  


Solution:2

You need to explicitly cast the return value of dlsym(), it returns a void* and desk is not of type void*. Like this:

desk = (double (*)(char*))dlsym(handle, "some_function");


Solution:3

Basically you need to apply a cast to the return value of dlsym when assigning it to desk. In this article, they cast the void* return to their function pointer.

Look at this article.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »