11
votes

I realize that this question has been asked many times on StackOverflow and on other sites; after reviewing these resources I am still at a loss.

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

The issue I am having is the same as many others have: 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:
* Using the 64-bit version from GLFW
* Using IDEs (Eclipse, VS)
* The suggestion in GLFW Undefined References
* Suggestions in What is an undefined reference/unresolved external symbol error and how do I fix it? (swapping linking argument order)
* Suggestion in OpenGL with Eclipse CDT + MinGW + GLEW + GLFW: Undefined References
* Attempted to use CMake to compile the libraries myself, but do not see any .a, .lib, or .dll files created in the process.

Please let me know if additional information would be helpful.

3
My suggestion is: instead of using a partial path (-L./lib), use a complete pathAmadeus
@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).Matt Martin

3 Answers

9
votes

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.

2
votes
g++ Test.cpp -lglfw -lGL -lm -lX11 -lpthread -lXi -lXrandr -ldl

Should work

1
votes

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.

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