1

I just dont get it, why line 22 is failing to compile?

#include <stdexcept> #include <dlfcn.h> #include "Library.h" int main(int argc, char *argv[]) { try { void* libHandle = 0; libHandle = dlopen("libExpandableTestLibrary.so", RTLD_LAZY); if(!libHandle) throw std::logic_error(dlerror()); std::cout << "Libary opened gracefully" << std::endl; void* fuPtr = 0; fuPtr = dlsym(libHandle, "createLibrary"); if(!fuPtr) throw std::logic_error(dlerror()); Library* libInstance = static_cast<Library* ()>(fuPtr)(); // Tutorial: http://www.linuxjournal.com/article/3687 // Tutorial Code: shape *my_shape = static_cast<shape *()>(mkr)(); // Compiler error message: Application.cpp:22:56: error: invalid static_cast from type ‘void*’ to type ‘Library*()’ libInstance->Foo(); dlclose(libHandle); } catch(std::exception& ex) { std::cerr << ex.what() << std::endl; } } 

Any help is welcome If you need additional information's just let me know.

5
  • Try a reinterpret_cast - void* and other casts don't mix. Commented Nov 23, 2011 at 16:45
  • @JohnDibling, it's inline in the code... Commented Nov 23, 2011 at 16:46
  • @Nim: not quite. to cast from void* to any other object pointer type (that was maybe previously implicitly converted to the void*) the proper way is a static_cast. Commented Nov 23, 2011 at 16:50
  • @PlasmaHH, with an object pointer possibly, but a function pointer too? Commented Nov 23, 2011 at 16:54
  • For people coming here wanting to create their dynamic class loading code, check this single-header library: github.com/davidalbertonogueira/… @Disclaimer: I'm the maintainer of said lib. Commented May 29, 2018 at 10:03

3 Answers 3

3

I take it that fuPtr points to a function that is supposed to return a pointer to a Library object (given the name loaded is "createLibrary").

In that case, the line including your cast needs to look like this:

Library* libInstance = reinterpret_cast<Library* (*)()>(fuPtr)(); 
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't work either as in that case you have to use reinterpret_cast however, you pointed me to the correct solution. Thanks in advance. Solution: Library* libInstance = reinterpret_cast<Library* (*)()>(fuPtr)();
Ah. Not having your library handy, I subbed in a test function that my compiler had no complaints about static_casting. But point taken.
0

invalid static_cast from type ‘void*’ to type ‘Library*()’

In C++ it is illegal to cast between object and function pointer types (because e.g. they could be of different size).

Most compilers that support this as an extension will require you to use a reinterpret_cast or even a c-style cast.

2 Comments

static_cast<>() requires a type as an argument. The type provided is not a type at all.
@bert-jan: It is a type, try code like: struct L; L*f(); it is just not a function pointer type, but a function type. See also template parameters like std::function<L*()> f;. Since any cast of that kind is a compiler extension, it is up to them whether it automatically decays to a pointer to function (like real functions do), or not. Depending on how his compiler implements such an extension, making it a pointer-to-function may or may not be necessary.
0

"Library* ()" does not evaluate to a type. Try "Library * (*)()"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.