0

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 ? :)

4

1 に答える 1

0

さて、私はなんとかそれを機能させました...「= 0;」「Boucle()」関数のインターフェースにありませんでした。

しかし、私は別の問題に直面しています...boucle()関数を呼び出すことはできますが、呼び出すたびにsegfaultが発生します...

私が使用するコードは次のとおりです。

    int main(int argc, char **argv)
    {
      IDisplay              *display;
      display = gestionnary.loadLibFromName(std::string(argv[3]));
      display->Boucle();
    }

そしてGDBは私にそれを教えてくれます:

Program received signal SIGSEGV, Segmentation fault.
0x000000000040b325 in main (argc=4, argv=0x7fffffffe538, env=0x7fffffffe560) at Core/main.cpp:44
44    display->Boucle();

Boucle() 関数は、フレーズの印刷のみで構成されています...

于 2013-03-20T15:11:38.127 に答える