Ok, the problem was during the compilation. After adding the flag -rdynamic, the code works perfectly.
Undefined symbol, access to a member function with a pointer C++
-
18-03-2022 - |
Question
I'd like to access to the member functions of my main class with a pointer, from a class in a lib.
I have access to the variables public in my main class. But when I try to access to the member functions (public), I have this error :
./Panda: symbol lookup error: libkoala.so: undefined symbol: _ZNK5Panda6getLOLEv
I really don't know why, maybe I don't have understand a notion in c++...
My main class:
class Panda
{
protected:
IAssistant* (*external_creator)();
IAssistant* lib;
void* dlhandle;
int lol;
public:
int hello;
Panda();
~Panda();
int getLOL() const;
};
Panda::Panda()
{
if ((dlhandle = dlopen("libkoala.so", RTLD_LAZY)) == NULL)
exit(-1);
if ((external_creator = reinterpret_cast<IAssistant* (*)()>(dlsym(dlhandle, "create_assistant"))) == NULL)
exit(-1);
lib = external_creator();
lol = 69;
hello = 42;
lib->do_something(this);
}
Panda::~Panda(){
}
int Panda::getLOL() const
{
return lol;
}
The interface looks like that:
class IAssistant
{
public:
virtual void do_something(Panda *) = 0;
};
And the class on my lib:
class Koala : public IAssistant
{
public:
void do_something(Panda *);
};
void Koala::do_something(Panda * ptr)
{
std::cout << ptr->hello; <========= work perfectly
std::cout << ptr->getLOL(); <====== doesn't work
}
extern "C"
{
IAssistant* create_assistant()
{
return new Koala();
}
}
Have you got any idea ?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow