Undefined references when compiling using MinGW

mrsimb wrote on Sunday, June 16, 2013:

Hello. I can’t compile a simple program.

#include <GLFW/glfw3.h>

void render();

int main(int argc, char **argv)
{
    GLFWwindow* window;
    glfwInit();
    window=glfwCreateWindow(640, 480, "Test", NULL, NULL);
    glfwMakeContextCurrent(window);
    
    while(!glfwWindowShouldClose(window))
    {
        render();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    
    glfwTerminate();
    return 0;
}

void render()
{
    glClear(GL_COLOR_BUFFER_BIT);
    
    glBegin(GL_TRIANGLES);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex2f(-1.0f, 0.0f);
    glVertex2f(0.0f, 1.0f);
    glVertex2f(1.0f, 0.0f);
    glEnd();
}

Mingw shell input:

mingw32-g++ main.cpp -o test.exe -I C:/Programming/GLFW3/include -L C:/Programming/GLFW3/lib-mingw -lmingw32 -lglfw3 -lopengl32

MinGW shell output:

C:/Programming/GLFW3/lib-mingw/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x30a): undefined reference to `_imp__CreateDCW@16'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x356): undefined reference to `_imp__GetDeviceCaps@8'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x3a2): undefined reference to `_imp__DeleteDC@4'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(win32_gamma.c.obj):win32_gamma.c:(.text+0x7b): undefined reference to `_imp__CreateDCW@16'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(win32_gamma.c.obj):win32_gamma.c:(.text+0x91): undefined reference to `_imp__GetDeviceGammaRamp@8'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(win32_gamma.c.obj):win32_gamma.c:(.text+0x9d): undefined reference to `_imp__DeleteDC@4'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(win32_gamma.c.obj):win32_gamma.c:(.text+0x35b): undefined reference to `_imp__CreateDCW@16'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(win32_gamma.c.obj):win32_gamma.c:(.text+0x36d): undefined reference to `_imp__SetDeviceGammaRamp@8'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(win32_gamma.c.obj):win32_gamma.c:(.text+0x379): undefined reference to `_imp__DeleteDC@4'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xeb): undefined reference to `_imp__DescribePixelFormat@16'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x939): undefined reference to `_imp__DescribePixelFormat@16'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xa70): undefined reference to `_imp__DescribePixelFormat@16'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xa92): undefined reference to `_imp__SetPixelFormat@12'
C:/Programming/GLFW3/lib-mingw/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1522): undefined reference to `_imp__SwapBuffers@4'
collect2.exe: error: ld returned 1 exit status

mrsimb wrote on Sunday, June 16, 2013:

Sorry for the horrible formatting, I don’t know why the text has turned a large size and has been cropped.

elmindreda wrote on Sunday, June 16, 2013:

You are using an old version of MinGW. You need to manually link against gdi32.

mrsimb wrote on Sunday, June 16, 2013:

Thank you very much.

hburd wrote on Monday, June 17, 2013:

Yes thank you it worked for me as well