Search code examples
c++macosopenglglfwglm-math

Failed to create GLFW window?


I tried to create a window with the simplest code possible:

#include <stdio.h>
#include <stdlib.h>

#include <GL/glew.h> // Always include it before glfw.h
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>

int main(void) {
    glfwInit();
    glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
    glfwWindowHint(GLFW_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); // Mac

    GLFWwindow* window = glfwCreateWindow(720, 480, "OpenGL", NULL, NULL); // Create a window

    if (window == NULL) {
        fprintf(stderr, "Failed to create window\n");
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    return 0;
}

This is my Makefile:

game:
    g++ src/main.cpp -std=c++17 -o play -I include -L lib -l glfw.3 -l GLEW.2.2

When I compile my code there is no error, but when I try to play my code I have this error:

Failed to create window
Invalid window hint 0x00000003
Invalid window hint 0x00000003
Context profiles are only defined for OpenGL version 3.2 and above

Can someone help me? I don't know why my window doesn't want to be created...


Solution

  • GLFW_VERSION_MAJOR & GLFW_VERSION_MINOR are not valid arguments to glfwWindowHint:

    glfwWindowHint(GLFW_VERSION_MAJOR, 3); // OpenGL 3.3
                   ^^^^^^^^^^^^^^^^^^ nope
    glfwWindowHint(GLFW_VERSION_MINOR, 3);
                   ^^^^^^^^^^^^^^^^^^ also nope
    

    Use GLFW_CONTEXT_VERSION_MAJOR & GLFW_CONTEXT_VERSION_MINOR instead.

    As per the docs, emphasis mine:

    GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR specify the client API version that the created context must be compatible with. The exact behavior of these hints depend on the requested client API.

    Note: Do not confuse these hints with GLFW_VERSION_MAJOR and GLFW_VERSION_MINOR, which provide the API version of the GLFW header.