I'm facing a little problem in C++.
So, I have a game, a sort of Snake, and I want to do it with three different graphic libraries. (like libsdl.so, libndk.so and libQt.so).
I have the following classes :
DisplaySDL.hh :
#ifndef DISPLAYSDL_HH__
# define DISPLAYSDL_HH__
#include "IDisplay.hh"
class DisplaySdl : public IDisplay
{
public:
DisplaySdl();
~DisplaySdl();
void Boucle(Core &core);
};
#endif
DisplaySDL.cpp:
#include <iostream>
#include "DisplaySdl.hh"
extern "C"
{
IDisplay* createDisplay()
{
return new DisplaySdl();
}
}
DisplaySdl::DisplaySdl()
{
std::cout << "SDL Loaded" << std::endl;
}
DisplaySdl::~DisplaySdl()
{
}
void DisplaySdl::Boucle(Core &core)
{
std::cout << "this is just a test" << std::endl;
}
And I have my Interface "IDisplay" :
#ifndef IDISPLAY_HH__
# define IDISPLAY_HH__
#include "../Core/Core.hh"
class IDisplay
{
public:
virtual ~IDisplay() {}
// virtual void dispSnake(Snake snake) = 0;
// virtual void dispBlock(Block block) = 0;
// virtual void dispMap(Map map) = 0;
virtual void Boucle(Core &core);
};
#endif
(I just put the DisplaySDL.hh and DisplaySDL.cpp as the other libs have the same design pattern / functions)
And here's the code that load the different libraries and create a IDisplay *
object. :
IDisplay* LibGestionnary::loadLibFromName(const std::string &libname)
{
IDisplay* (*external_creator)();
void* dlhandle;
dlhandle = dlopen(libname.c_str(), RTLD_LAZY);
if (dlhandle == NULL)
std::cout << dlerror() << std::endl;
external_creator = reinterpret_cast<IDisplay* (*)()>(dlsym(dlhandle, "createDisplay"));
if (external_creator == NULL)
std::cout << dlerror() << std::endl;
IDisplay* Display = external_creator();
dlclose(dlhandle);
return (Display);
}
The thing is that my function loadLibFromName() is working great, it loads the library that I tell it, BUT only when I don't have any function member in any of my graphic lib. If I remove the "boucle()" function from my code, it works great as shown below :
./nibbler 20 20 ./libsdl.so
SDL Loaded
Otherwise, that's what I get when I try to load the lib :
yanis@b3nd3r:~/Projets/C++/nibbler$ ./nibbler 20 20 ./libsdl.so
./libsdl.so: undefined symbol: _ZTI8IDisplay
./nibbler: undefined symbol: createDisplay
Segmentation Fault
Any help ? :)