问题描述
我正在遵循 GLFW 入门指南,但我似乎无法让它与 GLAD 一起运行.
I am following the GLFW guide to getting started but I can't seem to make it run with GLAD.
这是我的 C 文件 (prac.c)
Here's my C file (prac.c)
#include <stdio.h>
#include <stdlib.h>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
void error_callback(int error, const char* description) {
fprintf(stderr, "Error %d: %s
", error, description);
}
int main(void) {
GLFWwindow* window;
if(!glfwInit()) return -1;
glfwSetErrorCallback(error_callback);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if(!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
printf("OpenGL version: %s
", glGetString(GL_VERSION));
gladLoadGL();
while(!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
我通过运行这些命令的 Makefile 编译它:
And I compile it via a Makefile which runs these commands:
gcc -Wall -lglfw -lGL -ldl -Iglad/include -o obj/glad.o -c glad/src/glad.c
gcc -Wall -lglfw -lGL -ldl -Iglad/include -o obj/prac.o -c prac.c
gcc -Wall -lglfw -lGL -ldl -Iglad/include -o prac obj/glad.o obj/prac.o
每当我尝试运行可执行文件时,都会遇到分段错误.但是,当我注释掉 #include<glad/glad.h>
和 gladLoadGL();
并构建它时,它会运行.这使我相信包含 glad.h
时会发生分段错误,但对于我来说,我无法弄清楚为什么.
Whenever I try to run the executable, I get a segmentation fault. However, when I comment out #include<glad/glad.h>
and gladLoadGL();
and build it, it does run. This leads me to believe that the segmentation fault occurs when including glad.h
, but for the life of me I can't figure out why.
推荐答案
你必须先调用initialize glad调用任何 OpenGL 指令.
这意味着 gladLoadGL()
必须在 glGetString(GL_VERSION)
之前完成:
You have to call initialize glad before calling any OpenGL instruction.
That means gladLoadGL()
has to be done before glGetString(GL_VERSION)
:
glfwMakeContextCurrent(window);
gladLoadGL();
printf("OpenGL version: %s
", glGetString(GL_VERSION));
这篇关于包含glad.h时的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!