我在 Ubuntu 20.04 LTS 系统上,我无法运行我的 CPP 代码。每次我尝试编译这个:
#include <iostream>
#include <GLFW/glfw3.h>
#include "glad/glad.h"
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 800, "Endless Space", NULL, NULL);
if (window == NULL)
{
std::cout << "FAILED TO LAUNCH WINDOW! TERMINATING..." << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
do {
glfwPollEvents();
}
while (!glfwWindowShouldClose);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
通过这个命令:
g++ main.cpp -o EndlessSpace
我收到这个错误:
In file included from main.cpp:3:
glad/glad.h:27:2: error: #error OpenGL header already included, remove this include, glad already provides it
27 | #error OpenGL header already included, remove this include, glad already provides it
| ^~~~~
In file included from main.cpp:3:
glad/glad.h:1305: warning: "GL_INVALID_INDEX" redefined
1305 | #define GL_INVALID_INDEX 0xFFFFFFFF
|
In file included from /usr/include/GL/gl.h:2050,
from /usr/include/GLFW/glfw3.h:210,
from main.cpp:2:
/usr/include/GL/glext.h:1355: note: this is the location of the previous definition
1355 | #define GL_INVALID_INDEX 0xFFFFFFFFu
|
In file included from main.cpp:3:
glad/glad.h:1347: warning: "GL_TIMEOUT_IGNORED" redefined
1347 | #define GL_TIMEOUT_IGNORED 0xFFFFFFFFFFFFFFFF
|
In file included from /usr/include/GL/gl.h:2050,
from /usr/include/GLFW/glfw3.h:210,
from main.cpp:2:
/usr/include/GL/glext.h:1430: note: this is the location of the previous definition
1430 | #define GL_TIMEOUT_IGNORED 0xFFFFFFFFFFFFFFFFull
|
我实际上尝试删除 GLFW 标头,导致此错误:
main.cpp: In function ‘int main()’:
main.cpp:6:5: error: ‘glfwInit’ was not declared in this scope
6 | glfwInit();
| ^~~~~~~~
main.cpp:8:20: error: ‘GLFW_CONTEXT_VERSION_MAJOR’ was not declared in this scope
8 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:8:5: error: ‘glfwWindowHint’ was not declared in this scope
8 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
| ^~~~~~~~~~~~~~
main.cpp:9:20: error: ‘GLFW_CONTEXT_VERSION_MINOR’ was not declared in this scope
9 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:10:20: error: ‘GLFW_OPENGL_PROFILE’ was not declared in this scope
10 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
| ^~~~~~~~~~~~~~~~~~~
main.cpp:10:41: error: ‘GLFW_OPENGL_CORE_PROFILE’ was not declared in this scope
10 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
| ^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:12:5: error: ‘GLFWwindow’ was not declared in this scope
12 | GLFWwindow* window = glfwCreateWindow(800, 800, "Endless Space", NULL, NULL);
| ^~~~~~~~~~
main.cpp:12:17: error: ‘window’ was not declared in this scope
12 | GLFWwindow* window = glfwCreateWindow(800, 800, "Endless Space", NULL, NULL);
| ^~~~~~
main.cpp:12:26: error: ‘glfwCreateWindow’ was not declared in this scope
12 | GLFWwindow* window = glfwCreateWindow(800, 800, "Endless Space", NULL, NULL);
| ^~~~~~~~~~~~~~~~
main.cpp:17:9: error: ‘glfwTerminate’ was not declared in this scope
17 | glfwTerminate();
| ^~~~~~~~~~~~~
main.cpp:21:5: error: ‘glfwMakeContextCurrent’ was not declared in this scope
21 | glfwMakeContextCurrent(window);
| ^~~~~~~~~~~~~~~~~~~~~~
main.cpp:24:9: error: ‘glfwPollEvents’ was not declared in this scope
24 | glfwPollEvents();
| ^~~~~~~~~~~~~~
main.cpp:26:13: error: ‘glfwWindowShouldClose’ was not declared in this scope
26 | while (!glfwWindowShouldClose);
| ^~~~~~~~~~~~~~~~~~~~~
main.cpp:28:5: error: ‘glfwDestroyWindow’ was not declared in this scope
28 | glfwDestroyWindow(window);
| ^~~~~~~~~~~~~~~~~
main.cpp:29:5: error: ‘glfwTerminate’ was not declared in this scope
29 | glfwTerminate();
| ^~~~~~~~~~~~~
供您参考,我确实 DID 安装了 GLFW 并且......我真的不知道如何安装 GLAD,所以我并没有做太多。
请帮忙谢谢!!
尝试在主文件的顶部定义 GLFW_INCLUDE_NONE 与
#define GLFW_INCLUDE_NONE