Linking error with mingw (codeblocks)

I was trying to compile an example code, but couldn’t get past a linking error.

main.cpp(this code is from glfw documentation page):

#include <GLFW/glfw3.h>

int main(void)
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
        return -1;

    /* Make the window's context current */

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
        /* Render here */

        /* Swap front and back buffers */

        /* Poll for and process events */

    return 0;


I am getting two different linker errors:

  1. One is very large(multiple undefined type errors) and occurs when I link libglfw3.a from lib-mingw-w64 folder.

  2. The 2nd error is of single line:
    undefined reference to '__imp_glClear'
    it occurs when I link either:
    libglfw3dll.a FROM lib-mingw-w64 FOLDER or,
    glfw3.dll FROM lib-static-ucrt FOLDER

Removing the line glClear(GL_COLOR_BUFFER_BIT); from the while loop in main.cpp seems to get rid of the 2nd error and successfully compile. And it even gives a simple window when run.
However, this doesn’t feel right. Deleting a function to stop linker error.
I am new to opengl, glfw and am very clueless about any of the linking stuff so please bear with me. I just can’t figure out, am I forgetting something else to link? Or change some compiler settings maybe?
Details about the system:
Pre compiled glfw 64 bit windows library
Code blocks (empty project as the template)
msys64 mingw compiler

OpenGL functions are not part of glfw. You link to OpenGL library to get them. On Windows GL 1.1 functions are in OpenGL32.dll file.

To link to OpenGL32.dll file add -lopengl32 linker argument - it will make mingw to use libopengl32.a import library.

1 Like

Thank you very much. A very silly mistake on my side
to people using code blocks: add libopengl32.a in the “linker settings”. (assuming the lib folder of your compiler is already included in the search directories of the project)

It is worth noting you might need GDI instead (or as well). And the order of linking flags matters in MinGW-w64. So -lglfw3 -lgdi32 works but fails if you swap the flags.

1 Like

In my case it worked without linking gdi32