我在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();
| ^~~~~~~~~~~~~
型
为了你的信息,我确实 * 做 * 安装GLFW和.我不能真正弄清楚如何安装GLAD,所以我真的没有做太多。
请帮帮忙谢谢!
2条答案
按热度按时间xeufq47z1#
尝试在主文件的顶部定义GLFW_INCLUDE_NONE,
字符串
ehxuflar2#
你需要在glfw之前包括glad。
字符串
Glad包含OpenGL头文件,并显式定义错误(如果您已经在Glad之前加载了它们)。
从glad. h,第27行,这是你的错误所在(glad/glad.h:27:2:)
型
首先使用glad. h include的原因是,GLFW头会查看是否已经包含OpenGL头,然后不包含它。所以很高兴包含OpenGL头不会影响GLFW。
你也可以在Intenzy的答案中使用define,它阻止GLFW包含开发头。
GLFW入门页面也包含此信息,位于https://www.glfw.org/docs/3.3/quick.html