# Linking libglfw3.a undefined reference

**URL:** <https://discourse.glfw.org/t/linking-libglfw3-a-undefined-reference/2121>\
**Category:** support\
**Created:** [July 30, 2022, 1:03pm UTC](https://discourse.glfw.org/t/linking-libglfw3-a-undefined-reference/2121 "2022-07-30T13:03:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiegert](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@tiegert](https://discourse.glfw.org/u/tiegert)\
**Post date:** [July 30, 2022, 1:03pm UTC](https://discourse.glfw.org/t/linking-libglfw3-a-undefined-reference/2121/1 "2022-07-30T13:03:18Z")

</div>

Hi guys,  
i have just downloaded the sources for glfw-3.3.8 and used cmake and mingw32-make to compile the sources. So far so good.

But when i try to link the /build/src/libglfw3.a file like that:

```auto
g++ test.cpp C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a -o test

```

and then call any function, i get the following output in the terminal:

```auto
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x175): undefined reference to `__imp_CreateDCW'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x1ad): undefined reference to `__imp_GetDeviceCaps'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x1cb): undefined reference to `__imp_GetDeviceCaps'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x22b): undefined reference to `__imp_GetDeviceCaps'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x29c): undefined reference to `__imp_GetDeviceCaps'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x2c8): undefined reference to `__imp_DeleteDC'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0xc75): undefined reference to `__imp_GetDeviceCaps'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0xc8d): undefined reference to `__imp_GetDeviceCaps'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x12c1): undefined reference to `__imp_CreateDCW'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x12df): undefined reference to `__imp_GetDeviceGammaRamp'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x12f2): undefined reference to `__imp_DeleteDC'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x1451): undefined reference to `__imp_CreateDCW'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x146f): undefined reference to `__imp_SetDeviceGammaRamp'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_monitor.c.obj):win32_monitor.:(.text+0x1482): undefined reference to `__imp_DeleteDC'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x232): undefined reference to `__imp_CreateDIBSection'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x2a3): undefined reference to `__imp_CreateBitmap'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x2d2): undefined reference to `__imp_DeleteObject'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x3da): undefined reference to `__imp_DeleteObject'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x3ea): undefined reference to `__imp_DeleteObject'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0xc81): undefined reference to `__imp_CreateRectRgn'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0xce6): undefined reference to `__imp_DeleteObject'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x8fb): undefined reference to `__imp_DescribePixelFormat'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xe24): undefined reference to `__imp_DescribePixelFormat'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1234): undefined reference to `__imp_SwapBuffers'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1673): undefined reference to `__imp_ChoosePixelFormat'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x168c): undefined reference to `__imp_SetPixelFormat'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1b28): undefined reference to `__imp_DescribePixelFormat'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1b6e): undefined reference to `__imp_SetPixelFormat'
collect2.exe: error: ld returned 1 exit status

```

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 30, 2022, 3:13pm UTC](https://discourse.glfw.org/t/linking-libglfw3-a-undefined-reference/2121/2 "2022-07-30T15:13:22Z")

</div>

See the guide on [building applications with MinGW](https://www.glfw.org/docs/latest/build_guide.html#build_link_win32), in particular the line:

_When using MinGW to link an application with the static version of GLFW, you must also explicitly link with **gdi32**. Other toolchains including MinGW-w64 include it in the set of default libraries along with other dependencies like user32 and kernel32._

So you should write (note this is untested):

```auto
g++ test.cpp C:\git\opengl\glfw-3.3.8\glfw-3.3.8\build\src\libglfw3.a -lgdi32 -o test

```

---

<div class="post-metadata">

**Author:** ![tiegert](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@tiegert](https://discourse.glfw.org/u/tiegert)\
**Post date:** [July 30, 2022, 4:29pm UTC](https://discourse.glfw.org/t/linking-libglfw3-a-undefined-reference/2121/3 "2022-07-30T16:29:54Z")

</div>

thanks, that did the trick!  
meanwhile i have also found out that linking the dll and not the .a does also work.

For everybody wondering, to get the dll from the sources, you need to call cmake with  
`-D BUILD_SHARED=ON`
