user101375
user101375

Reputation: 7071

Using C++ Library in Linux (eclipse)

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.

Upvotes: 1

Views: 941

Answers (3)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84169

In C++ you need the following:


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

Upvotes: 3

kbyrd
kbyrd

Reputation: 3351

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");

Upvotes: 2

Doug T.
Doug T.

Reputation: 65599

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.

Upvotes: 0

Related Questions