glad/glad.h:27:2:错误:#error OpenGL标头已包含,删除此包含,很高兴已经提供了它

问题描述 投票:0回答:2

我使用的是 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,所以我并没有对此做太多事情。

请帮忙谢谢!!

c++ g++ glfw
2个回答
0
投票

尝试在主文件顶部定义 GLFW_INCLUDE_NONE 与

#define GLFW_INCLUDE_NONE

0
投票

您需要在 glfw 之前添加glad。

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

Glad 包含 OpenGL 标头,并显式定义错误(如果您在 Glad 有机会之前已加载它们)。

来自glad.h,第27行,这是你的错误所指向的地方(glad/glad.h:27:2:)

#ifdef __gl_h_
#error OpenGL header already included, remove this include, glad already provides it
#endif
#define __gl_h_

首先包含glad.h 的原因是GLFW 标头会查看OpenGL 标头是否已包含,然后不包含它。很高兴包含 OpenGL 标头不会影响 GLFW。

您还可以使用 Intenzy 答案中的定义,这会阻止 GLFW 包含开发标头。

GLFW 入门页面也包含此信息,位于 https://www.glfw.org/docs/3.3/quick.html

© www.soinside.com 2019 - 2024. All rights reserved.