Home > Software engineering >  Segmentation Fault GLFW3/GLAD
Segmentation Fault GLFW3/GLAD

Time:05-29

I have a really weird segmentation fault when trying to create a window using GLFW3.

Linux kernel: 5.4.0-113-generic

NVIDIA drivers: 510.73.05

OpenGL (from glxinfo): OpenGL core profile version string: 4.5 (Core Profile) Mesa 21.2.6

GLAD: C/C OpenGL 4.5 Core (No Extensions)

GLFW: 3.3.7

I am using this nifty command to compile:

gcc src/*.c -Wall -Ideps/glad/include -Ideps/glfw/include -Ldeps/glad/lib -Ldeps/glfw/lib -lglad -lglfw3 -lGL -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lm -o bin/balldrop

And all I've done is built GLFW3 using cmake -S . -B. && make and isolated the libglfw3.a and the include directory so that it's all that's left, and compiled the glad.c file into libglad.a to make it statically bindable as well. No errors or warnings.

Hoping that's enough context. Anyway, I'm simply running this code:

#include <stdio.h>
#include <stdlib.h>
#include <glad/glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
int main() {
    GLFWwindow* window;
    if(!glfwInit()) return -1;
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if(!window) {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    while(!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

And I get an immediate segfault upon compilation. Running gdb I see the segfault is just requesting some address 0x0000000000000000 @ ?? while also referencing the main address. I'm honestly not quite sure what this could be. My drivers are up to date, my kernel is up to date, the drivers support OpenGL 4.5, and I added all the command line flags to get the darn thing to compile.

CodePudding user response:

You're using OpenGL calls (glClear) before initializing glad. You have to do gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) before that. This is mentioned in the getting started guide of GLFW.

  • Related