sako-is
sako-is

Reputation: 41

GLFW doesn't initialize on Window using clang

I was trying to initialize a window using GLFW on Windows 11, but it failed with no error. The code compiled but when I ran it, nothing happened. I compiled GLFW using clang in Visual Studio 17 2022 and compiled the program itself with this command: clang -std=c18 -g -IC:\GLFW\include -LC:\GLFW\build\src\Release\ -lglfw3dll .\main.c -o test.exe

Here's the code:

#include <stdio.h>
#include <GLFW/glfw3.h>

static void error_callback(int error, const char* description) {
    fprintf(stderr, "Error: %s\n", description);
}

int main() {
    glfwSetErrorCallback(error_callback);
    int status = glfwInit();

    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

    GLFWwindow* window = glfwCreateWindow(800, 600, "Test", NULL, NULL);

    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
    return 0;
}

Edit: The code works on Fedora 37 btw

Upvotes: 0

Views: 222

Answers (1)

G.M.
G.M.

Reputation: 12879

If I take your code and add the call...

glfwMakeContextCurrent(window);

I get the following error message at the terminal...

Error: Cannot make current with a window that has no OpenGL or OpenGL ES context

Removing the call...

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);

fixes that and the code then behaves as I would expect. The complete code is now...

#include <stdio.h>
#include <GLFW/glfw3.h>

static void error_callback(int error, const char* description) {
  fprintf(stderr, "Error: %s\n", description);
}

int main() {
    glfwSetErrorCallback(error_callback);
    int status = glfwInit();

    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

    GLFWwindow* window = glfwCreateWindow(800, 600, "Test", NULL, NULL);

    glfwMakeContextCurrent(window);

    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
    return 0;
}

Upvotes: 1

Related Questions