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: GLFW: Building applications

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

Hello! I wrote gcc main.c libglfw3.a -lopengl32 -lkernel32 -luser32 -lgdi32 -lws2_32 and I got linker error telling me that it can’t find glfw functions. I have libglfw3.a file in the same directory in which main.c is. What should I do?

Hi @student99 welcome to the GLFW forum,

The most common error with MinGW (it looks like you are using that) is that you need to use the correct bit GLFW library, which depends on which MinGW you are using and not on which version of Windows you are using.

So if you have the 64bit version of MinGW you need the 64bit download, otherwise the 32bit download. So if you have Windows 64bit but have MinGW 32 bit then you need the 32bit binaries or switch to 64bit MinGW.

If you are still stuck you could try using the following cmake GLFW starter which has everything you need set up:

I would also recommend using Visual Studio Community edition as the free native compiler on Windows over MinGW unless you are an expert.

1 Like