ubuntu GLFW错误#65544“无法初始化GLFW”

jaql4c8m  于 2023-04-20  发布在  其他
关注(0)|答案(1)|浏览(1088)

下面是我代码:

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

int main(void){

    GLFWwindow* window;

    if(!glfwInit()){
        std::cout << "Failed to initialize GLFW!" << std::endl;
        return -1;
    }

    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if(!window){
        glfwTerminate();
        std:: cout << "Failed to initialize Window!" << std::endl;
        return -1;
    }

    glfwMakeContextCurrent(window);

    while(!glfwWindowShouldClose(window)){
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

我成功地链接并编译了它,但我有一个运行时错误,它说glfw无法初始化。我试图在C中使用glfw,但这个错误显示

Wayland: Failed to connect to display
The GLFW library is not initialized
main: ./src/posix_thread.c:64: _glfwPlatformGetTls: Assertion `tls->posix.allocated == GLFW_TRUE' failed.
Aborted (core dumped)

我使用的是popos22.04LTS,我是c++和glfw的新手,请帮我解决这个问题

fivyi3re

fivyi3re1#

问题

你得到这些错误的原因是因为glfw框架在被用来创建窗口之前没有得到初始化的机会。因此,glfw返回给定的错误消息。

Wayland: Failed to connect to display
The GLFW library is not initialized
main: ./src/posix_thread.c:64: _glfwPlatformGetTls: Assertion `tls-posix.allocated == GLFW_TRUE' failed.
Aborted (core dumped)

解决方案

若要修复此问题,请尝试移动代码段

if(!glfwInit()){
    std::cout << "Failed to initialize GLFW!" << std::endl;
    return -1;
}

在main函数开头的GLFWwindow* window声明上方。

快速提示

尝试在一行中声明和初始化窗口,如下所示:GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "GLFW WINDOW", NULL, NULL)它更容易阅读,总体上有助于减少代码行数。

相关问题