glewInit()为什么会导致段错误?

huangapple go评论60阅读模式
英文:

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 &lt;GL/glew.h&gt;
#include &lt;GLFW/glfw3.h&gt;

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, &quot;Spinning Cube&quot;, 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);

huangapple
  • 本文由 发表于 2023年1月9日 05:14:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定