繁体   English   中英

共享库和成员函数

[英]Shared library and member functions

我在C ++中遇到了一个小问题。

所以,我有一个游戏,一种Snake,我想用三个不同的图形库来做。 (例如libsdl.so,libndk.so和libQt.so)。

我有以下课程:

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;
}

我有我的界面“ 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

(我只是将DisplaySDL.hh和DisplaySDL.cpp放在其他库具有相同的设计模式/功能的地方)

这是加载不同库并创建IDisplay *对象的代码。

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);
}

关键是我的函数loadLibFromName()可以很好地工作,它会加载我告诉它的库,但是只有当我的图形库中没有任何函数成员时,它才会加载。 如果我从代码中删除“ boucle()”函数,则它的工作原理如下所示:

./nibbler 20 20 ./libsdl.so
SDL Loaded

否则,这就是我尝试加载lib时得到的结果:

yanis@b3nd3r:~/Projets/C++/nibbler$ ./nibbler 20 20 ./libsdl.so 
./libsdl.so: undefined symbol: _ZTI8IDisplay
./nibbler: undefined symbol: createDisplay
Segmentation Fault

有什么帮助吗? :)

好吧,我设法使其正常工作……“ = 0;” 我的界面中缺少“ Boucle()”函数。

但是我面临另一个问题...我可以调用我的boucle()函数,但是每当执行此操作时,我都会遇到段错误...

这是我使用的代码:

    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()函数仅包含在打印短语时...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM