I have problem with dynamic loading function with C++ (in C it works).
Let test.cpp be
Code:
#include <iostream>
#include <string>
using namespace std;
void f(string s)
{
cout << s << endl;
}
and main.cpp
Code:
#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);
}
Then, I made libtest.so with
Code:
g++ -c test.c
g++ -shared -fPIC -o libtest.so test.o
put it into /usr/lib and set appropriate file attributes. But,
gives error main.cpp:12: error: invalid conversion from `void*' to `void (*)(std::string)'.
Why? Same thing works in C with char* and printf() instead of string and cout.