opengl-esshaderopengl-es-2.0undefined-symbol

Conditionally include GLES2 where needed


I'm trying to use functions such as glCreateShader, glGetShaderiv, glDeleteProgram but they don't exist when I import GL/gl.h or GL/glext.h on a Linux system.

This is probably because its a laptop from 2008 with the integrated graphics reporting:

2.1 Mesa 20.1.10

and the discrete card:

3.0 Mesa 20.1.10

When I import GLES2/gl2.h then everything works fine and the includes are resolved.

Now I don't currently have access to a desktop computer or one with sufficiently higher graphics standard for at least a month - I'm assuming that these better systems have shader functionality included in the standard GL/gl.h file.

How can I write a conditional import to require GLES only where needed? I don't want to have to add a variable to the Makefile, I want to be able to automatically be able to detect a system where these symbols are only available as GLES components at compile time with #ifdef sequences.


Solution

  • You have to use an OpenGL loader like glew or glad. OpenGL (ES) is a specification. The loader gives you access to the API functions which are provided by the graphics driver.

    See OpenGL Loading Library - glad:

    const SDL_GLContext context = SDL_GL_CreateContext(window);
    if (context == nullptr) {
        std::cout << "SDL could not create context";
        return 1;
    }
    
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize OpenGL context" << std::endl;
        return -1;
    }
    

    See Initialize GLEW:

    const SDL_GLContext context = SDL_GL_CreateContext(window);
    if (context == nullptr) {
        std::cout << "SDL could not create context";
        return 1;
    }
    
    if {glewInit() != GLEW_OK}
    {
        std::cout << "Failed to initialize GLEW" << std::endl;
        return -1;
    }