0
我在C++中遇到了一些小问题。共享库和成员函数
所以,我有一个游戏,一种蛇的,我想用三个不同的图形库来做。 (如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
(我只是把Disp laySDL.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()是伟大的工作,它加载的,我告诉它的图书馆,但只有当我没有任何我的图形lib中的任何功能部件。 如果我删除从我的代码中的“仿羔皮呢()”函数,它的伟大工程,如下图所示:
./nibbler 20 20 ./libsdl.so
SDL Loaded
否则,这就是我得到的,当我尝试加载的lib:
[email protected]:~/Projets/C++/nibbler$ ./nibbler 20 20 ./libsdl.so
./libsdl.so: undefined symbol: _ZTI8IDisplay
./nibbler: undefined symbol: createDisplay
Segmentation Fault
任何帮助? :)