glewInit and building dll

glewInit and building dll

本文关键字:dll building and glewInit      更新时间:2023-10-16

我正在开发爱好opengl3引擎,我决定对它进行一个新的.dll构建。它使用 GLEW 从 GPU 获取 opengl3 函数。大约 5 个月前我已经成功构建了它,但现在我已经改变了很多,它不想工作。

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

在伪代码中:

#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: %dn", id);          //so this one is never called

所以我的问题是,为什么 GLEW 只能在不是从我的.dll导入的代码中工作,我该如何解决它?

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

感谢您的帮助!

#define GLEW_STATIC

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

要构建.dll请改用#define GLEW_BUILD