这是我的代码:
#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)
.我正在使用 popOS 22.04 LTS,我是 c++ 的新手,glfw 请帮我解决这个问题
您收到这些错误的原因是因为 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)
它更容易阅读,总体上有助于减少代码行数。