c++ glad/glad.h:27:2:error:#error OpenGL header already included,remove this include,glad already provides it

svgewumm  于 2024-01-09  发布在  其他
关注(0)|答案(2)|浏览(155)

我在Ubuntu 20.04 LTS系统上,我无法运行CPP代码。每次我试图编译这个:

  1. #include <iostream>
  2. #include <GLFW/glfw3.h>
  3. #include "glad/glad.h"
  4. int main()
  5. {
  6. glfwInit();
  7. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  8. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  9. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  10. GLFWwindow* window = glfwCreateWindow(800, 800, "Endless Space", NULL, NULL);
  11. if (window == NULL)
  12. {
  13. std::cout << "FAILED TO LAUNCH WINDOW! TERMINATING..." << std::endl;
  14. glfwTerminate();
  15. return -1;
  16. }
  17. glfwMakeContextCurrent(window);
  18. do {
  19. glfwPollEvents();
  20. }
  21. while (!glfwWindowShouldClose);
  22. glfwDestroyWindow(window);
  23. glfwTerminate();
  24. return 0;
  25. }

字符串
通过此命令:g++ main.cpp -o EndlessSpace
我收到这个错误:

  1. In file included from main.cpp:3:
  2. glad/glad.h:27:2: error: #error OpenGL header already included, remove this include, glad already provides it
  3. 27 | #error OpenGL header already included, remove this include, glad already provides it
  4. | ^~~~~
  5. In file included from main.cpp:3:
  6. glad/glad.h:1305: warning: "GL_INVALID_INDEX" redefined
  7. 1305 | #define GL_INVALID_INDEX 0xFFFFFFFF
  8. |
  9. In file included from /usr/include/GL/gl.h:2050,
  10. from /usr/include/GLFW/glfw3.h:210,
  11. from main.cpp:2:
  12. /usr/include/GL/glext.h:1355: note: this is the location of the previous definition
  13. 1355 | #define GL_INVALID_INDEX 0xFFFFFFFFu
  14. |
  15. In file included from main.cpp:3:
  16. glad/glad.h:1347: warning: "GL_TIMEOUT_IGNORED" redefined
  17. 1347 | #define GL_TIMEOUT_IGNORED 0xFFFFFFFFFFFFFFFF
  18. |
  19. In file included from /usr/include/GL/gl.h:2050,
  20. from /usr/include/GLFW/glfw3.h:210,
  21. from main.cpp:2:
  22. /usr/include/GL/glext.h:1430: note: this is the location of the previous definition
  23. 1430 | #define GL_TIMEOUT_IGNORED 0xFFFFFFFFFFFFFFFFull
  24. |


事实上,我已经尝试删除GLFW头,导致此错误:

  1. main.cpp: In function int main()’:
  2. main.cpp:6:5: error: glfwInit was not declared in this scope
  3. 6 | glfwInit();
  4. | ^~~~~~~~
  5. main.cpp:8:20: error: GLFW_CONTEXT_VERSION_MAJOR was not declared in this scope
  6. 8 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  7. | ^~~~~~~~~~~~~~~~~~~~~~~~~~
  8. main.cpp:8:5: error: glfwWindowHint was not declared in this scope
  9. 8 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  10. | ^~~~~~~~~~~~~~
  11. main.cpp:9:20: error: GLFW_CONTEXT_VERSION_MINOR was not declared in this scope
  12. 9 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  13. | ^~~~~~~~~~~~~~~~~~~~~~~~~~
  14. main.cpp:10:20: error: GLFW_OPENGL_PROFILE was not declared in this scope
  15. 10 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  16. | ^~~~~~~~~~~~~~~~~~~
  17. main.cpp:10:41: error: GLFW_OPENGL_CORE_PROFILE was not declared in this scope
  18. 10 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  19. | ^~~~~~~~~~~~~~~~~~~~~~~~
  20. main.cpp:12:5: error: GLFWwindow was not declared in this scope
  21. 12 | GLFWwindow* window = glfwCreateWindow(800, 800, "Endless Space", NULL, NULL);
  22. | ^~~~~~~~~~
  23. main.cpp:12:17: error: window was not declared in this scope
  24. 12 | GLFWwindow* window = glfwCreateWindow(800, 800, "Endless Space", NULL, NULL);
  25. | ^~~~~~
  26. main.cpp:12:26: error: glfwCreateWindow was not declared in this scope
  27. 12 | GLFWwindow* window = glfwCreateWindow(800, 800, "Endless Space", NULL, NULL);
  28. | ^~~~~~~~~~~~~~~~
  29. main.cpp:17:9: error: glfwTerminate was not declared in this scope
  30. 17 | glfwTerminate();
  31. | ^~~~~~~~~~~~~
  32. main.cpp:21:5: error: glfwMakeContextCurrent was not declared in this scope
  33. 21 | glfwMakeContextCurrent(window);
  34. | ^~~~~~~~~~~~~~~~~~~~~~
  35. main.cpp:24:9: error: glfwPollEvents was not declared in this scope
  36. 24 | glfwPollEvents();
  37. | ^~~~~~~~~~~~~~
  38. main.cpp:26:13: error: glfwWindowShouldClose was not declared in this scope
  39. 26 | while (!glfwWindowShouldClose);
  40. | ^~~~~~~~~~~~~~~~~~~~~
  41. main.cpp:28:5: error: glfwDestroyWindow was not declared in this scope
  42. 28 | glfwDestroyWindow(window);
  43. | ^~~~~~~~~~~~~~~~~
  44. main.cpp:29:5: error: glfwTerminate was not declared in this scope
  45. 29 | glfwTerminate();
  46. | ^~~~~~~~~~~~~


为了你的信息,我确实 * 做 * 安装GLFW和.我不能真正弄清楚如何安装GLAD,所以我真的没有做太多。
请帮帮忙谢谢!

xeufq47z

xeufq47z1#

尝试在主文件的顶部定义GLFW_INCLUDE_NONE,

  1. #define GLFW_INCLUDE_NONE

字符串

ehxuflar

ehxuflar2#

你需要在glfw之前包括glad。

  1. #include <glad/glad.h>
  2. #include <GLFW/glfw3.h>

字符串
Glad包含OpenGL头文件,并显式定义错误(如果您已经在Glad之前加载了它们)。
从glad. h,第27行,这是你的错误所在(glad/glad.h:27:2:)

  1. #ifdef __gl_h_
  2. #error OpenGL header already included, remove this include, glad already provides it
  3. #endif
  4. #define __gl_h_


首先使用glad. h include的原因是,GLFW头会查看是否已经包含OpenGL头,然后不包含它。所以很高兴包含OpenGL头不会影响GLFW。
你也可以在Intenzy的答案中使用define,它阻止GLFW包含开发头。
GLFW入门页面也包含此信息,位于https://www.glfw.org/docs/3.3/quick.html

展开查看全部

相关问题