2013-03-20 42 views
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 

任何幫助? :)

回答

0

好吧,我設法得到它的工作......在「= 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(); 

的仿羔皮呢()函數中包含只對短語的打印...