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
", dlerror());
exit(1);
}
create = (MyClass* (*)()) dlsym(lib_handle, "create_object");
if ((error = dlerror()) != NULL)
{
fprintf(stderr, "%s
", 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.
See Question&Answers more detail:os