Assume a dynamic library exports a function, e.g. of type void()
, named foo
. A client code could then make use of it like in the following snippet (assuming foo
is exposed via extern "C" void foo();
for simplicity)
#include "Foo.hpp" // defines the type of foo
// dlopen a library and check it was found
// look for a symbol foo in a library lib
void * fooPtr = dlsym(lib, "foo");
// if reading the symbol succeeded
if (!dlerror()) {
// use it
using Foo = decltype(foo);
((Foo*)fooPtr)();
}
I've understood that static_cast
is not usable here, but is the above C-style cast the way to go?