Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

opengl - How to include <glad/glad.h> into several header files in C++?

I have a project in C++ using opengl. I have main.cpp working with openGL initialization, creating window and stuff like that. I would like to create a class, where I can outsource loading some texture into Shader. But when I am trying to use shader.h or glad.h headers as include in any other class-headers, I got error:

fatal error C1189: #error: OpenGL header already included, remove this include, glad already provides it

If I do all logic inside main.cpp everything is fine, problem raised only if trying to use openGL functions anywhere except main.cpp

main.cpp:

#include <glad/glad.h> // generated from https://glad.dav1d.de
#include "shader.h"

int main()
{
     ... //Do OpenGL Staff
}

shader.h:

#ifndef SHADER_H
#define SHADER_H

#include <glad/glad.h>
#include <glm/glm.hpp>


class Shader
{
public:
    unsigned int ID;
...//some Shader definition staff
}
#endif

and now I want external class "Maze.h" to load it's map inside opengl texture, something like That:

class Maze
{
public:
    ...//some maze-relate staff
    void LoadMazeToGL(Shader* shader)
   {
        // load and create a texture 
        // -------------------------
        glGenTextures(1, &screenTex1);
        glBindTexture(GL_TEXTURE_1D, screenTex1);
        // set the texture wrapping parameters
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);   // set texture wrapping to GL_REPEAT (default wrapping method)
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        // set texture filtering parameters
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        GLint curMapId = glGetUniformLocation(shader->ID, "shaderInternalVarName");
        glUniform1i(curMapId, 2); // Texture unit 2 is for current map.

        ... //define and fill tex1data using Maze private information
    
        glActiveTexture(GL_TEXTURE0 + 2);
        glBindTexture(GL_TEXTURE_1D, screenTex1);
        glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, MazeIntegerSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex1data);
    
    }
}
question from:https://stackoverflow.com/questions/65890118/how-to-include-glad-glad-h-into-several-header-files-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To check for problems like this use the -E flag of GCC/g++ or an alternative for your compiler. This outputs the result of the pre-processor (so the includes etc). You can then have a look at the files produced and look for the double include. You may have just forgotten an include guard somewhere :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...