英文:
Why does glewInit() give me a segfault?
问题
我在使用OpenGL、glew和glfw开发一个项目。
这个项目比较庞大,但只有初始化部分的代码不能正常工作。我使用的是glew 2.2.0_1和glfw 3.3.8。
顺便说一下,我在调试器中运行过,所以我百分之百确定glewInit()导致了段错误。而且如果我将这部分代码放在一个没有其他代码的文件中,也会导致段错误。
这是代码:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const int WIDTH = 640;
const int HEIGHT = 480;
int main(int argc, char **argv) {
// 设置GLFW并创建窗口
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Spinning Cube", nullptr, nullptr);
glfwMakeContextCurrent(window);
if (!window) {
return 1;
}
// 设置GLEW
glewInit();
// ...
}
这是一个库错误还是我做错了什么?(对于任何询问,这段代码可以编译而不会出现任何警告或错误)
英文:
Im working on a project with opengl using glew and glfw
The project is larger but only the initialisation code doesnt work correctly. Im using glew 2.2.0_1 and glfw 3.3.8
(btw. I ran this in a debugger so Im 100% sure the glewInit() is causing the segfault. And if I put this in a file without the rest of the code its also causing a segfault)
The code is:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const int WIDTH = 640;
const int HEIGHT = 480;
int main(int argc, char **argv) {
// Set up GLFW and create window
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Spinning Cube", nullptr, nullptr);
glfwMakeContextCurrent(window);
if (!window) {
return 1;
}
// Set up GLEW
glewInit();
// ...
}
Is it a library error or am I doing something wrong? (For anybody asking this code compiles without any warnings or errors)
答案1
得分: 1
在 macOS 上,您必须创建一个带有前向兼容标志的OpenGL Core Profile上下文:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
英文:
On macOS, you must create an OpenGL Core Profile context with the forward compatibility flag set:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论