Undefined reference to '__mingw_free' / '__mingw_realloc'

I’m trying to compile my code, but getting errors.

Source code:

#include <GLFW/glfw3.h>

int main()
{
	if (glfwInit()) glfwTerminate();
	return 0;
}

Command line:
gcc main.cpp -IC:\libs\GLFW\include -LC:\libs\GLFW\libs\lib-mingw\x86 -lglfw3 -lgdi32

Output:

C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(init.c.obj):init.c:(.text+0xb9): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(init.c.obj):init.c:(.text+0xda): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(init.c.obj):init.c:(.text+0x11c): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(init.c.obj):init.c:(.text+0x2bf): undefined reference to `__mingw_realloc'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(input.c.obj):input.c:(.text+0x1016): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(input.c.obj):input.c:(.text+0x1021): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(input.c.obj):input.c:(.text+0x102c): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(input.c.obj):input.c:(.text+0x242c): undefined reference to `__mingw_realloc'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(input.c.obj):input.c:(.text+0x17f6): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0xc2): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0xd0): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0xde): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x114): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x122): more undefined references to `__mingw_free' follow
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x2bd): undefined reference to `__mingw_realloc'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x392): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x3a0): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x3ae): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x3e4): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x3f2): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(monitor.c.obj):monitor.c:(.text+0x400): more undefined references to `__mingw_free' follow
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xe46): undefined reference to `__mingw_realloc'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x1091): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x10c9): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x10d9): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x202d): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x23a5): undefined reference to `__mingw_free'
C:\libs\GLFW\libs\lib-mingw\x86/libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x521c): more undefined references to `__mingw_free' follow
collect2.exe: error: ld returned 1 exit status

GLFW version: 3.3.9 x86 (precompiled)
Operating system: Windows 11 x64
Compiler: MinGW x86

Note: MinGW is not the easiest way to compile on Windows, the free MSVC compiler might be an easier path if you are not an expert.

First off I note you are using gcc (C compiler) rather than g++ (C++ compiler) to compile main.cpp which is C++.

The undefined references are part of the standard library, after first trying g++ I would check that your version of the compiler is at least 9.2.0 as this is what GLFW was built using, see this Stackoverflow answer. The readme.txt in the downloaded binary version of GLFW has the compiler version in.

An alternative approach might be to try this GLFW-CMake-starter.

Thanx! Here it is!
An official MinGW installer is gave me very outdated version. I’m not knew about it.
But why old compiler unable to link the static library (-lglfw3) only? :thinking: It’s linking the -lglfw3dll successfully.

This is because of the difference in what a static library and dynamic library are.

Static libraries are just bundles of compiled code - they are not ‘linked’ to their dependencies [1] since they require a compile toolchain to be used. Dynamic libraries are linked to their dependencies as they can be independently updated or dynamically loaded on an end users machine.

[1] There are some caveats to this, but this answer is sufficient to explain the difference you are seeing.

1 Like