2

I'm trying to access function from dynamic library, that instantiates an instance of Person, and returns pointer to it as void pointer. The program then has to cast the void pointer to a Person, using a reinterpret_cast. But, I'm getting an error: error: ‘void*’ is not a pointer-to-object type.

Here is the code:

function from library:

void* loadPerson (void) {
    return reinterpret_cast<void*>(new Person);
}

main.cpp:

void* loadPerson = dlsym(lib_handle, "loadPerson");
void* person_vp = (*loadPerson)();
Person* person = reinterpret_cast<Person*>(person_vp);

if (dlerror() != NULL) 
   cout<<"Library init error."<<endl;  
else {
   //...

Thanks!

1 Answers1

2

The problematic line is:

void* person_vp = (*loadPerson)();

You're dereferencing a void*. You need this:

void* person_vp = (*reinterpret_cast<void* (*)()>(loadPerson))();

EDIT:

For better readability, the cast can be split like this:

typedef void* VoidFunc();
VoidFunc* loadPerson_func = reinterpret_cast<VoidFunc*>(loadPerson);
void* person_vp = (*loadPerson_func)();
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • Good answer, but I would add a variable for the function pointer so the process is more clear. – Jonathan Grynspan Dec 06 '12 at 13:33
  • @JonathanGrynspan Thanks, expanded the answer. – Angew is no longer proud of SO Dec 06 '12 at 13:42
  • @Agnew I'm now getting an error in NetBeans: `RUN FAILED (exit value 1)`. According to [this](http://www.drdobbs.com/dynamically-loaded-c-objects/184401900) tutorial, I need to call function like `(*loadPerson)()` because it returns value, that is assigned to `person_vp` pointer. What am I missing here? –  Dec 06 '12 at 14:12
  • @edin.gacan Never used NetBeans with C++, but I guess the message could be that your `main()` returned `1`. Is that possible? Also, you should probably check that `dlsym()` returns a non-null pointer. – Angew is no longer proud of SO Dec 06 '12 at 14:50
  • @Angew Nope, `main()` returns `0`, and `dlsym()` also returns value. –  Dec 07 '12 at 08:08
  • @edin.gacan That will take someone who knows NetBeans, then. What does it do when run directly? Or in a debugger? – Angew is no longer proud of SO Dec 07 '12 at 09:15
  • @Angew nothing, just `exit value 1`. I don't understand meaning of `*` before `reinterpret_cast`, can you please explain it to me? Thanks :) –  Dec 07 '12 at 14:21
  • @edin.gacan It should be clear if you look at the multi-line version. The destination type of the `reinterpret_cast` is a pointer (to function returning `void*` and taking no arguments). The `*` before the cast is just normal dereference operator - we want to dereference the pointer which came from the cast. – Angew is no longer proud of SO Dec 07 '12 at 14:41
  • Yeah, I think I get it. We use `*` to dereference pointer which came from the cast, so we can call the function, am I right? Here is modified code, but still no success: –  Dec 07 '12 at 14:53
  • `void* loadPerson = dlsym(lib_handle, "loadPerson");` `if (dlerror() != NULL) cout<<"Symbol load error."<(loadPerson)) (); Person* p = reinterpret_cast(test); }` –  Dec 07 '12 at 14:56
  • @edin.gacan Yes, we dereference the pointer to call the function. Your new problems seem unrelated to the compilation error you had; I think you'd better post them as a new question. – Angew is no longer proud of SO Dec 07 '12 at 15:02