I have problem with dynamic loading function with C++ (in C it works).
Let test.cpp be
and main.cppCode:#include <iostream> #include <string> using namespace std; void f(string s) { cout << s << endl; }
Then, I made libtest.so withCode:#include <dlfcn.h> #include <string> using namespace std; extern "C" void f(string); int main() { void* handle = dlopen("libtest.so", RTLD_LAZY); void (*test)(string) = dlsym(handle, "f"); (*test)("Hello, World!"); dlclose(handle); }
put it into /usr/lib and set appropriate file attributes. But,Code:g++ -c test.c g++ -shared -fPIC -o libtest.so test.o
gives error main.cpp:12: error: invalid conversion from `void*' to `void (*)(std::string)'.Code:g++ -ldl main.cpp
Why? Same thing works in C with char* and printf() instead of string and cout.



LinkBack URL
About LinkBacks



CornedBee