OpenGL GLFW: undefined reference to 'glfwInit'
Asked Answered
G

5

20

I am simply trying to get OpenGL working on my machine (Windows 7 64-bit) with GLFW.

I am getting the singular linker error:

undefined reference to 'glfwInit'.

The code I am trying to compile is the simplest possible (in a file Test.cpp).

#include <iostream>
#include <GLFW/glfw3.h>
int main()
{
    std::cout << "hello world" << std::endl;
    glfwInit();
    return 0;
}

I am using a simple Makefile to attempt to compile:

Test: Test.o
    g++ -o Test  -L./lib -lglew32 -lglfw3 -lopengl32 -lglu32 -lgdi32 Test.o

Test.o: Test.cpp
    g++ -I./include -c Test.cpp

Additional information:

  • Using g++ to compile (MinGW32)
  • The lib folder contains glfw3.dll, libglfw3.a, and libglfw3dll.a (Win32 version downloaded from GLFW website - Windows pre-compiled library)
  • The include folder contains a folder named GLFW, which contains glfw3.h and glfw3native.h (from downloaded GLFW - include folder)

I have tried:

Goodloe answered 17/4, 2017 at 5:59 Comment(2)
My suggestion is: instead of using a partial path (-L./lib), use a complete pathAmusement
@Amadeus: Thanks for the suggestion. Gave it a shot, but nothing seems to have changed (it seems that the linker is able to locate all the libraries correctly - it gives a different error if It is unable to locate glfw3).Goodloe
G
12

Finally figured out my issue MANY hours later.

Leaving the libglfw3.a file in the same directory as the glfw3.dll (if attempting dynamic linking) will confuse the linker. Delete it if linking dynamically - all you need is the dll and the /include folder.

Also, add

#define GLFW_DLL

above the include statement if linking with a DLL.

Goodloe answered 18/4, 2017 at 6:0 Comment(0)
H
5
g++ Test.cpp -lglfw -lGL -lm -lX11 -lpthread -lXi -lXrandr -ldl

Should work

Himyaritic answered 19/8, 2020 at 11:36 Comment(1)
I added -ldl for dynamic linkingHimyaritic
R
2

If you link all libraries with GCC correctly and no errors appear, but it just doesn't define functions like "glfwInit()", check what version of MinGW you have. If you use 86x version of MinGW then library has to be 86x as well. Same probably goes for 64x, but I didn't check yet.

To check the version of your MinGW, use gcc --version command in the command line.

I was trying to fix that problem for whole day and this was the reason. Really hope i helped someone with that.

Ravelin answered 11/5, 2019 at 14:6 Comment(1)
It's good to add how it's possible to check version of MinGW in command prompt. It's under gcc -vAlchemist
B
2

I had this same problem and I found an answer on the glfw forums.

https://discourse.glfw.org/t/linking-compiling-glfw-with-mingw/1969/4

I was using the static '.a' library with MinGW g++. In your case, you decided to use the shared '.dll' library, but for those that are going the static route this may still help. It turns out that in my case it was an issue with my makefile. When the linker looks for defined references, it needs the file with the definitions to be linked after the file that references them.

For example, this doesn't work:

g++ -o main.exe -Ipath/to/included/files -Lpath/to/libraries -lglfw3 -lgdi32 main.cpp  

It gets to main.cpp last and there is nothing after main.cpp to define the glfw functions it uses. This, on the other hand, works:

g++ -o main.exe -Ipath/to/included/files -Lpath/to/libraries main.cpp -lglfw3 -lgdi32  

The linker gets to main.cpp and starts looking for those references before it gets to the glfw3 library, where they are defined. No error.

You get a similar error if you try to put the -lgdi32 in front of the -lglfw3. The glfw3 library has references to the gdi32 library, so it needs to go in front for the linker to find their definitions.

Bosworth answered 6/3, 2023 at 22:16 Comment(0)
R
0

I had to add the glfw to target link libraries.

target_link_libraries( executable_name glfw )

Rheumatoid answered 17/8, 2022 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.