0

I have read through many linker error problems but I have not managed to resolve mine. I am able to link GLFW dynamically but not statically.

My setup

  • Compiler: g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0
  • OS:
    • WindowsProductName: Windows 10 Home
    • WindowsVersion: 2009
  • CLI: Powershell

My Application.cpp from the GLFW 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)
        {
            glfwTerminate();
            return -1;
        }
    
        /* Make the window's context current */
        glfwMakeContextCurrent(window);
    
        /* Loop until the user closes the window */
        while (!glfwWindowShouldClose(window))
        {
            /* Render here */
            glClear(GL_COLOR_BUFFER_BIT);
    
            /* Swap front and back buffers */
            glfwSwapBuffers(window);
    
            /* Poll for and process events */
            glfwPollEvents();
        }
    
        glfwTerminate();
        return 0;
}

My file structure

In the build directory there are the dll's to link the Application.exe which is build from the first command from the "CLI command" - section below. Furthermore I have included both lib-mingw and lib-mingw-w64 because I am unsure which is necessary for my MinGW version. I have read that the first one should be the one I need.

    ├───build
    │       glfw3.dll
    │       libglfw3.a
    │       libglfw3dll.a
    │
    ├───Dependencies
    │   └───GLFW
    │       ├───include
    │       │   └───GLFW
    │       │           glfw3.h
    │       │           glfw3native.h
    │       │
    │       ├───lib-mingw
    │       │       glfw3.dll
    │       │       libglfw3.a
    │       │       libglfw3dll.a
    │       │
    │       └───lib-mingw-w64
    │               glfw3.dll
    │               libglfw3.a
    │               libglfw3dll.a
    │
    └───src
            Application.cpp

CLI command

  • This works (dynamic linking):
  1. g++ -g -I.\Dependencies\GLFW\include\ .\src\Application.cpp -o .\build\Application.exe -L.\Dependencies\GLFW\lib-mingw\ -lglfw3dll -lopengl32 Furthermore I wonder why -lopengl32 is necessary for dynamic linking if it is not mentioned in here Building applications - With MinGW or Visual C++ on Windows.
  • This don't work (static linking):
  1. g++ -g -I.\Dependencies\GLFW\include\ .\src\Application.cpp -o .\build\Application.exe -L.\Dependencies\GLFW\lib-mingw\ -lglfw3 -lopengl32

  2. g++ -g -I.\Dependencies\GLFW\include\ .\src\Application.cpp -o .\build\Application.exe -L.\Dependencies\GLFW\lib-mingw\ -lglfw3 -lopengl32 -lgdi32 -luser32 -lkernel32 don't work as well as mentioned in Building applications - With MinGW or Visual C++ on Windows.

Error Code

.\Dependencies\GLFW\lib-mingw\/libglfw3.a(init.c.obj):init.c:(.text+0xb9): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(init.c.obj):init.c:(.text+0xda): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(init.c.obj):init.c:(.text+0x11c): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(init.c.obj):init.c:(.text+0x2bf): undefined reference to `__mingw_realloc'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(input.c.obj):input.c:(.text+0x1016): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(input.c.obj):input.c:(.text+0x1021): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(input.c.obj):input.c:(.text+0x102c): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(input.c.obj):input.c:(.text+0x240c): undefined reference to `__mingw_realloc'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(input.c.obj):input.c:(.text+0x17d6): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0xc2): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0xd0): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0xde): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x114): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x122): more undefined references to `__mingw_free' follow
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x2bd): undefined reference to `__mingw_realloc'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x392): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x3a0): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x3ae): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x3e4): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x3f2): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x400): more undefined references to `__mingw_free' follow
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x113): undefined reference to `CreateDCW@16'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x16b): undefined reference to `GetDeviceCaps@8'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1c6): undefined reference to `GetDeviceCaps@8'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1fa): undefined reference to `DeleteDC@4'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x21f): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x35c): undefined reference to `GetDeviceCaps@8'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x373): undefined reference to `GetDeviceCaps@8'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x623): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x9a5): undefined reference to `GetDeviceCaps@8'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x9bc): undefined reference to `GetDeviceCaps@8'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xb6d): undefined reference to `GetDeviceCaps@8'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xb84): undefined reference to `GetDeviceCaps@8'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xe46): undefined reference to `__mingw_realloc'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xfe8): undefined reference to `CreateDCW@16'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xffd): undefined reference to `GetDeviceGammaRamp@8'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1008): undefined reference to `DeleteDC@4'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x116d): undefined reference to `CreateDCW@16'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x117e): undefined reference to `SetDeviceGammaRamp@8'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1189): undefined reference to `DeleteDC@4'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x25f): undefined reference to `CreateDIBSection@24'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x2ac): undefined reference to `CreateBitmap@20'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x34a): undefined reference to `DeleteObject@4'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x355): undefined reference to `DeleteObject@4'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x3fa): undefined reference to `DeleteObject@4'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x4c3): undefined reference to `CreateRectRgn@16'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x502): undefined reference to `DeleteObject@4'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0xf31): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0xf69): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0xf7b): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x1fbc): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x23c9): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x505c): more undefined references to `__mingw_free' follow
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x14a): undefined reference to `SwapBuffers@4'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x424): undefined reference to `ChoosePixelFormat@8'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x437): undefined reference to `SetPixelFormat@12'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xc30): undefined reference to `DescribePixelFormat@16'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xdf4): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xe25): undefined reference to `DescribePixelFormat@16'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xe4f): undefined reference to `SetPixelFormat@12'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x147c): undefined reference to `DescribePixelFormat@16'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x153e): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1594): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(egl_context.c.obj):egl_context.c:(.text+0xbde): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(egl_context.c.obj):egl_context.c:(.text+0xbe6): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(egl_context.c.obj):egl_context.c:(.text+0x1258): undefined reference to `__mingw_free'
.\Dependencies\GLFW\lib-mingw\/libglfw3.a(egl_context.c.obj):egl_context.c:(.text+0x1264): more undefined references to `__mingw_free' follow
collect2.exe: error: ld returned 1 exit status

lmixa
  • 49
  • 5
  • 1
    GetDeviceCaps needs to link to gdi32.lib. See the requirements part of this msdn documentation: [https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getdevicecaps#requirements](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getdevicecaps#requirements) – drescherjm Apr 10 '23 at 18:05
  • I was able to solve it and link it statically with `g++ -o .\build\Application.exe .\src\Application.cpp -I.\Dependencies\GLFW\include\ -L.\Dependencies\GLFW\lib-mingw-w64 -lglfw3 -lopengl32 -lgdi32` after changing my compiler to msys2 and the 64-bit binaries with lib-mingw-w64. – lmixa Apr 17 '23 at 15:56

0 Answers0