Linking GLFW on the command line not working

Hello, I’ve been trying to use GLFW for the past few weeks, but I can’t figure out how to resolve this error.

Some details: I’ve downloaded the 32-bit pre-compiled binaries

This was the command I entered to link the libglfw file:
gcc main.c lib-mingw\libglfw3.a

It gave a bunch of errors, but I don’t know what’s the problem.

Here is the explorer

See following section in documentation:

When using MinGW to link an application with the static version of GLFW, you must also explicitly link with gdi32 .

Hello, thanks I tried that with the command: gcc main.c lib-mingw\libglfw3.a -l gdi32

It got rid of most of the errors, but their’s still one left called:
undefined reference to `_imp__glClear@4’

That is OpenGL function, for that you need to link to OpenGL import library: -lopengl32

1 Like

Hello I also have an other question whenever I do:

gcc main.c Dependencies\glfw\lib-mingw\libglfw3.a -lglfw3 -lopengl32 -lkernel32 -luser32 -lgdi32 -lws2_32

It says:…/mingw32/bin/ld.exe: cannot find -lglfw3

How do I make the linker know that glfw3 does exist?

If you have libglfw3.a there with full path you don’t need -lglfw3 there. You need to link to only one glfw3 library

Oh when I don’t include the fully path though it says undefined reference here was my command.

gcc main.c -lopengl32 -lkernel32 -luser32 -lgdi32 -lws2_32

This is what it said:
C:\Users\COOLSH~1\AppData\Local\Temp\ccyGRAnW.o:main.c:(.text+0x17): undefined reference to glfwInit' C:\Users\COOLSH~1\AppData\Local\Temp\ccyGRAnW.o:main.c:(.text+0x51): undefined reference to glfwCreateWindow’
C:\Users\COOLSH~1\AppData\Local\Temp\ccyGRAnW.o:main.c:(.text+0x5f): undefined reference to glfwTerminate' C:\Users\COOLSH~1\AppData\Local\Temp\ccyGRAnW.o:main.c:(.text+0x74): undefined reference to glfwMakeContextCurrent’
C:\Users\COOLSH~1\AppData\Local\Temp\ccyGRAnW.o:main.c:(.text+0x100): undefined reference to glfwSwapBuffers' C:\Users\COOLSH~1\AppData\Local\Temp\ccyGRAnW.o:main.c:(.text+0x105): undefined reference to glfwPollEvents’
C:\Users\COOLSH~1\AppData\Local\Temp\ccyGRAnW.o:main.c:(.text+0x110): undefined reference to glfwWindowShouldClose' C:\Users\COOLSH~1\AppData\Local\Temp\ccyGRAnW.o:main.c:(.text+0x11d): undefined reference to glfwTerminate’

Also I feel like i’m doing something wrong here, cause my post keep getting flagged is it because I need to take it to Pm’s or something?

You misunderstood me.

I’m saying if you have Dependencies\glfw\lib-mingw\libglfw3.a argument there, then there is no need for -lglfw3. But you have both of them.

So this is incorrect:

gcc main.c Dependencies\glfw\lib-mingw\libglfw3.a -lglfw3 -lopengl32 -lkernel32 -luser32 -lgdi32 -lws2_32

But this is correct:

gcc main.c Dependencies\glfw\lib-mingw\libglfw3.a -lopengl32 -lkernel32 -luser32 -lgdi32 -lws2_32

Ohh sorry. Is there a way to do it without including the whole path and just doing -lglfw3?

For example, something like this:
gcc main.c
-lopengl32 -lkernel32 -luser32 -lgdi32 -lws2_32

There are two options.

  1. Put path to libraries in -L argument, then you can leave -lgfw32:
gcc main.c -LDependencies\glfw\lib-mingw -lglfw3 -lopengl32 -lkernel32 -luser32 -lgdi32 -lws2_32
  1. Copy libglfw3.a file next to main.c (in current folder), then you can use libglfw3.a directly in argument list:
gcc main.c libglfw3.a -lopengl32 -lkernel32 -luser32 -lgdi32 -lws2_32
1 Like