c++openglglfwglad

GLADloadProc is undeclared after having glew and glfw included


when trying to init glad using gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) I am receiving the error of USE OF UNDECLARED IDENTIFIER GLAD

code:


#include <iostream>
#include <[APP_NAME]/Game/ELGame.h>

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

void framebuffer_size_callback(GLFWwindow* window, int width, int height);  
void processInput(GLFWwindow *window);

int main(int, char**)
{
    glfwInit();

    int major = 4;
    int minor = 1;

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    // gotta stay apple friendly
    #ifdef __APPLE__
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    #endif
    
    GLFWwindow* window = glfwCreateWindow(900, 600, "[APP_NAME]", NULL ,NULL);
    
    if (window == NULL)
    { ... }


    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) // <- issue
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
    const GLubyte* version = glGetString(GL_VERSION); // version as a string
    //  printf("Renderer: %s\n", renderer);
    //  printf("OpenGL version supported %s\n", version);



    ...



    return 0;

at first I thought this was an issue caused by glad.c not being compiled however I it it included in my Cmake add_executables(). i have tried having this within project and from /usr/library/ ... /glad.c. I have also tried uncommenting the glad include at the top but that seems to give me an error saying that glad is already included in glfw / glew In included file : OpenGL header already included, remove this include, glad already provides it.


Solution

  • Both GLEW and GLAD are alternatives to the standard OpenGL header, that can properly handle the loading of the modern OpenGL functions. You will need to stick with one and only one for your project.

    In either case, the header for the loader (GLEW or GLAD) needs to be included before anything that would include the standard OpenGL header (as GLFW or similar library will generally do).

    To give a little more detail, the standard OpenGL header, GLEW header, and GLAD header all check if __gl_h is defined. If not, they will define it. So whichever gets to it first 'wins'.