0

I know this issue is quite common on SO but none of the other answers seem to work me. Here is the documentation for GLFW for building applications with MinGW, which is what I want to do. I made a minimum reproducible example here:

test.c:

#include <glfw3.h>

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

And here is what I want to do:

gcc -c test.c -Iinclude
gcc test.o -lglfw3 -lgdi32 -o test.exe

And here is the error is gives me:

test.o:test.c:(.text+0xc): undefined reference to `glfwInit'
test.o:test.c:(.text+0x11): undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status

List of things I have tried:

  • I have tried both the 32bit and 64bit glfw3 static libraries.
  • I have tried also linking other libraries references on the documentation I specified earlier such as kernel32 and user32 with no change to the error.
  • I have tried changing the order in which I link libraries.

I am slightly confused with this error, are the definitions for glfwInit and glfwTerminate not somewhere in the libglfw3.a? Surely they are? But, if so why does the linker throw undefined reference error?

Yazid Med
  • 67
  • 7
ripytide
  • 327
  • 2
  • 14
  • @user253751 I have it in the current working directory when I run the gcc commands, I also tried messing up the name to check that gcc did find the correct file. – ripytide Feb 18 '21 at 14:29

2 Answers2

0

the problems whose title starts with undefined reference are automatically problems related to the editing of links (the generation of your final binary due to the link editor, which takes place at the very end of the compilation process )

you have to make sure that every required library is not only present on your system, but also that your build instruction does indeed take the required library. Under Gcc, this corresponds to the -l option (note, it's a lowercase L and not an uppercase i;))

Yazid Med
  • 67
  • 7
0

I found the solution to this problem was to give the file name of libglfw3.a directly in the gcc arguments instead of using the -l argument so the working second gcc command should be:

gcc test.o libglfw3.a -lgdi32

ripytide
  • 327
  • 2
  • 14