Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL GLFW: undefined reference to 'glfwInit'

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.

like image 430
Matt Martin Avatar asked Apr 17 '17 05:04

Matt Martin


People also ask

Can I use GLFW with C++?

3.1 - What compilers are supported by GLFW? Currently, GLFW releases are tested with MinGW, MinGW-w64 and Visual C++ 2010, 2012, 2013 and 2015, but it should work with any compiler that supports C99 (C89 on Windows). Very old development environments may require updated system headers.

Do you need glad for GLFW?

Be sure to include GLAD before GLFW. The include file for GLAD includes the required OpenGL headers behind the scenes (like GL/gl. h ) so be sure to include GLAD before other header files that require OpenGL (like GLFW).

Does GLFW work with C?

GLFW is an Open Source, multi-platform library for OpenGL, OpenGL ES and Vulkan development on the desktop. It provides a simple API for creating windows, contexts and surfaces, receiving input and events. GLFW is written in C and supports Windows, macOS, X11 and Wayland.


2 Answers

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.

like image 172
Matt Martin Avatar answered Sep 22 '22 16:09

Matt Martin


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

Should work

like image 34
Mehul Nirala Avatar answered Sep 20 '22 16:09

Mehul Nirala