I am getting undefined symbol error while loading library dynamically.
Here is my code snippet that generates this error :
int main ()
{
void *lib_handle = NULL;
MyClass* (*create)();
void (*destroy)(MyClass*);
char *error;
lib_handle = dlopen ("./libshared.so", RTLD_LAZY);
if (lib_handle == NULL)
{
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
create = (MyClass* (*)()) dlsym(lib_handle, "create_object");
if ((error = dlerror()) != NULL)
{
fprintf(stderr, "%s\n", error);
exit(1);
}
destroy = (void (*)(MyClass*)) dlsym(lib_handle, "destroy_object");
MyClass *myClass = (MyClass*) create;
destroy(myClass);
dlclose(lib_handle);
}
But when I load library simply by commenting above code and exporting library path everything works like charm.
For dynamic linking I am using the following command on command prompt.
g++ -Wl,--export-dynamic shared_user.cpp -ldl
Any help would be appreciated.
extern "C" { create_object(...) ... }in your code. Beyond that, you need to invoke it ascreate()because you're not performing a function call otherwise. - FrankH.extern "C" { ... }bit in the sourcecode forlibshared.soto get the symbol names right. Also, if you want to test whether my hypothesis is correct, simply run "nm libshared.so" and see what the actual names are. - FrankH.