OpenGL GLFW: undefined reference to 'glfwInit'

Matt Martin picture Matt Martin · Apr 17, 2017 · Viewed 16.7k times · Source

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.

Answer

Matt Martin picture Matt Martin · Apr 18, 2017

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.