Problem while creating a window in C++ with GLFW 3.3.8 | Visual Studio 2019

155 Views Asked by At

i just installed glfw and i was testing the little example project for creating a window, and it just dosn't work, when i create the window, it exits the programm with a "-1" cuz the window is not created, what can i do? Edit: I fixed it alone :) i just change the compiler to x86 and change a bit the code xd

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <iostream>

int main(void)
{
    /* Initialize GLFW Library */
    if (!glfwInit()) {
        std::cout<<"ERROR: While initializing GLFW!"<<std::endl;
        exit(-1);
    }

    /* Create a windowed mode window and its OpenGL context */
    auto* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        std::cout<<"ERROR: While creating Window Object!"<<std::endl;
        glfwTerminate();
        exit(-1);
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Initialize GLEW Library */
    GLenum err = glewInit();
    if (err != GLEW_OK) {
        fprintf(stderr,"ERROR: %s\n", 
        glewGetErrorString(err));
        exit(-1);
    }
    fprintf(stdout,"Using GLEW %s\n", 
    glewGetString(GLEW_VERSION));

    //TODO: Create and compile shaders here (vertex and frament shaders)
    // and finally draw something with moder OpenGL!

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    exit(0);
}
2

There are 2 best solutions below

1
Light7734 On

You need to initialize glfw before creating a window

Call glfwInit() before glfwCreateWindow

0
Stupid Dev On

So, if you get the same error, please, change it to x86 in your compiler options, and then, dont initialize the window before de "glfwInit()" or else it will not work