Search code examples
c++openglmingwglfwglad

GLAD link is not providing needed functions


TLDR: The GLAD header file won't let me use the openGL Commands and I don't know exactly why.

Here's a breakdown:

  • I'm on a Windows 10 Laptop.

  • I'm coding in C++.

  • I'm compiling with MinGW's g++ tool.

  • I'm using GLFW and GLAD.

  • My file layout is something like this:

    OpenGLTest
        include
            glad
                glad.h
            GLFW
                glfw3.h
                glfw3native.h
            KHR
                khrplatform.h
        glad.c
        glfw3.dll
        GraphicsTest.cpp(Main File)
        libglfw3dll.a(I have forgotten what this does, it's a remnant from older attempts at openGL. Relevant?)
        Makefile(a single command: "g++ GraphicsTest.cpp -g -L glad.c glfw3.dll")
    
  • As far as I can tell, the program will compile and run flawlessly if any and all commands from GLAD and openGL are commented out. All it then does is make a small window.

  • If said lines are not commented out, the compiler will throw a slew of errors, all following this form:

    D:\Documents\Programming\C++\Programs\OpenGLTest/GraphicsTest.cpp:23: undefined reference to `gladLoadGL'
    

    ...with gladLoadGL being replaced with the relevant function.

  • The file itself reads thusly:

    #include<iostream>
    #include<glad\glad.h>
    #include<GLFW\glfw3.h>
    
    static void whatIsGoingOnSeriouslyGuysHelp(int id,const char* desc)
    {
        std::cout<<desc;
    }
    int main()
    {
        glfwSetErrorCallback(&whatIsGoingOnSeriouslyGuysHelp);
        glfwInit();
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    
        GLFWwindow* window = glfwCreateWindow(100,100,"TEST",NULL,NULL);
        if (window == NULL)
        {
            std::cout<<"Window Creation Error";
        }
        glfwMakeContextCurrent(window);
        gladLoadGL();
        //glViewport(0,0,100,100);
    
        while (!glfwWindowShouldClose(window))
        {
            //std::cout<<"?";
            glfwPollEvents();
            glfwSwapBuffers(window);
            //glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            //glClear(GL_COLOR_BUFFER_BIT);
        }
        glfwDestroyWindow(window);
        glfwTerminate();
        return 0;
    }
    

I have tried re-installing GLAD, but that didn't do anything. I haven't tried re-installing GLFW, but I don't think that's the problem.

Correct any misconceptions I have or errors I am making.


Solution

  • You need to include glad.c in your compile command as follows: (you had a -L before it which tells gcc to treat glad.c as a directory for libraries)

    g++ GraphicsTest.cpp glad.c -g glfw3.dll
    

    , and instead of gladLoadGL, you should use the GLFW loader:

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

    Finally, you should invest a tiny bit of time into a decent build system because compiling every file every time will get real slow real fast. Here is the absolute minimal Makefile to get you started:

    graphicstest: graphicstest.o glad.o
      g++ -g -o $@ $^ glfw3.dll