Search code examples
c++g++glfw

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


I am on a Ubuntu 20.04 LTS system, and I cannot get my CPP code to run. Everytime I try to compile this:

#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;
}

Via this command: g++ main.cpp -o EndlessSpace

I receive this error:

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
      | 

I have in-fact tried removing the GLFW header, resulting in this error:

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();
      |     ^~~~~~~~~~~~~

For your information, I indeed DID install GLFW and... I couldn't really figure out how to install GLAD, so I didn't really do much about that.

Please help thank you!!


Solution

  • You need to include glad before glfw.

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

    Glad includes the OpenGL headers, and explicitly defines errors if you already loaded them before glad has a chance to.

    From glad.h, line 27, which is where your error is pointing (glad/glad.h:27:2:)

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

    The reason having the glad.h include first works, is that the GLFW header looks to see if the OpenGL header is included already, and then does not include it. So glad including the OpenGL header doesn't affect GLFW.

    You can also use the define in Intenzy's answer, which stops GLFW from including the development header.

    The GLFW Getting Started page includes this information as well, at https://www.glfw.org/docs/3.3/quick.html