glewInit和构建DLL

时间:2017-07-03 14:33:08

标签: c++ opengl dll glew dllexport

我正在开发业余爱好的opengl3引擎,我决定制作一个新的.dll版本。它使用GLEW从GPU获取opengl3函数。我大约5个月前成功地建造了它,但现在我已经改变了很多,并且它不想工作。

所以.dll构建完美(windows7,mingw)。 我创建了一个简单的程序,它在第一次调用glCreateProgram时崩溃,该调用是由mylib.dll中的代码运行的。

伪代码中的

#include "mylib.hpp"
int main(){
   std::cout << (void*)glCreateProgram << "\n";  
   // displays 0 as glCreateProgram haven't been loaded yet

   myspace::Window* = new SDL2BasedWindow(...);
   //this constructor is from my .dll and calls glewInit() which returns GLEW_OK
   std::cout << (void*)glCreateProgram << "\n";
   //this displays proper address 
   int testGLEW= glCreateProgram();
   std::cout << "glCreateProgram from main: " << testGLEW<< "\n";
   //this displays 1 which means valid call to glCreateProgram
   window->runApplication(new Application(...));
   //Application is user-defined class, it further creates myspace::ShaderProgram 
   //which is imported from my .dll (part of my engine) which then calls 
   //glCreateProgram in it's initialisation 
   //(it is first call to any function which should be initialized by GLEW if we count only code imported from mylib.dll)


}

    //in ShaderProgram constructor:
    std::cout << "trying to call glCreateProgram, address: ";
    std::cout << (void*)glCreateProgram << "\n";  //this displays 0 (!)
    int id = glCreateProgram();                   //this ends execution with SIGSEGV
    printf("created program: %d\n", id);          //so this one is never called

所以我的问题是,为什么GLEW仅适用于未从我的.dll导入的代码,我该如何解决?

顺便说一下,我已经检查了nm mylib.dll ant包含glCreateProgram和其他glew依赖函数,我还在.dll和使用此.dll的程序中使用#define GLEW_STATIC

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

#define GLEW_STATIC用于构建静态库或可执行文件。

建立.dll使用#define GLEW_BUILD代替。