1

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.

Ty Q.
  • 207
  • 2
  • 13

1 Answers1

3

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.

kotatsuyaki
  • 1,441
  • 3
  • 10
  • 17
  • 1
    Yeah I actually just figured that out as of posting this... go figure, after a couple hours. Anyway, I thought GLFW3 initialized it automatically since it wouldn't take the param, but apparently you just call `gladLoadGL()` without any params and it'll compile ok. Thank you! Edit: If moderators don't remove this I'll mark your answer as correct when I can. – Ty Q. May 29 '22 at 03:29
  • I recall having to manually cast like this to make it compile if it does not: `gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)`. I'll update the answer to this. – kotatsuyaki May 29 '22 at 03:31
  • I dunno latest version of GLAD's gladLoadGL() doesn't accept any parameters. That method doesn't work. I'm honestly just happy it doesn't segfault upon compilation now. :) – Ty Q. May 29 '22 at 03:33
  • I'm not sure which version is the "latest version" of GLAD that you referred to. From [the master branch of glad](https://github.com/Dav1dde/glad/tree/ea756f7cc5e11dcef3cafdab87d45b3b528c875d) as of the time writing this comment, their readme still suggests passing `glfwGetProcAddress` as the parameter. – kotatsuyaki May 29 '22 at 03:36
  • I realized that we're talking about different functions of glad and you are right. `gladLoadGL` is a function that's different from `gladLoadGLLoader`, and it takes no parameters. – kotatsuyaki May 29 '22 at 03:39
  • That makes sense. Just weird how GLFW3's website still suggests using `gladLoadGL` with the `glfwGetProcAddress` instead of `gladLoadGLLoader`. Probably a mistake on their end? – Ty Q. May 29 '22 at 03:47