I need to dynamically link to a library function at runtime in Mac OS X. Following Apple's example, I declare a function pointer and assign it with the result of dlsym(). The following example compiles successfully as a plain C (.c) file. But I need this in a C++ file, and if I compile this example as a C++ file (.cpp), the clang compiler tells me
Cannot initialize a variable of type 'void ()(char *)' with an rvalue of type 'void '
Why does it work in plain 'C', and how can I fix this?
#include <dlfcn.h>
void Test() {
// Load the library which defines myFunc
void* lib_handle = dlopen("myLib.dylib", RTLD_LOCAL|RTLD_LAZY);
// The following line is an error if compiled as C++
void (*myFunc)(char*) = dlsym(lib_handle, "myFunc");
myFunc("Hello");
dlclose(lib_handle) ;
}
static_cast
: #310951 – Rollback