10

I'm trying to learn OpenGL with GLFW, but I'm having some problems.

This is my main.cpp:

#include <GL/glfw.h>

int main()
{
    glfwInit();
    glfwSleep( 1.0 );
    glfwTerminate();
}

This is my project's folder structure:

Project
+- glfw.dll
+- main.cpp

This is where I extracted the GLFW files:

MinGW
+- include
|   +- GL
|      +- glfw.h
+- lib
   +- libglfw.a
   +- libglfwdll.a

And this is how I try to build the program:

g++ main.cpp -o main.exe -lglfwdll

And these are the errors I'm getting:

C:\Users\Dark\AppData\Local\Temp\cc0ZgTVp.o:main.cpp:(.text+0xf): undefined reference to `_glfwInit'
C:\Users\Dark\AppData\Local\Temp\cc0ZgTVp.o:main.cpp:(.text+0x25): undefined reference to `_glfwSleep'
C:\Users\Dark\AppData\Local\Temp\cc0ZgTVp.o:main.cpp:(.text+0x2a): undefined reference to `_glfwTerminate'
collect2.exe: error: ld returned 1 exit status

Am I missing something?

Darkwater
  • 1,358
  • 2
  • 12
  • 25
  • use the big L option to specify the path where libraries are, like this `g++ ... -L /path/to/lib` also remember that MinGW uses a different set of ABIs from Visual Studio, so be sure that all the libraries that you are using for MinGW are compiled for MinGW. – Ken Oct 14 '12 at 21:20
  • I've tried a lot of folders for -L including `D:\Dropbox\C++\Untitled` where glfw.dll is located, but without success, and tbe .zip file I downloaded had a separate `lib-mingw` folder, so I assume these are the right libraries. – Darkwater Oct 14 '12 at 21:25
  • codeblocks uses MinGW under Windows, so i suppose that you can fix your problems just reading this http://stackoverflow.com/questions/7856648/glfw-and-codeblocks – Ken Oct 14 '12 at 21:27
  • I've done everything from the upvoted answer, except step 6: "Now while creating the GLFW project in code::blocks give the path C:\Program Files\CodeBlocks\MinGW" for glfw location" because that's a Code::Blocks specific step. – Darkwater Oct 14 '12 at 21:34
  • `g++ -print-search-dirs` this command will print all the paths considered by g++, if the lib it's not here you need to add it with the `-L` option. There can be other problems but I'm currently not programming on Windows, and I'm not touching Windows since days and months, but you can get really nasty behaviours like user restrictions from the UAC or different ABI standards. If you want to solve this just use Codeblocks and you will use the same MinGW that you are using now and, at least, you got support for it. Most of this problems are just caused by the Windows OS and not by MinGW itself. – Ken Oct 14 '12 at 21:41
  • 1
    I've tried a hundred solutions now and gave up. I'm going to try SFML now. – Darkwater Oct 15 '12 at 08:36
  • Another solution could be to use Visual Studio. These are linking problems with the libraries and unfortunately you'll still get link errors in Visual Studio but they are some what easy to fix – Dextron Aug 18 '20 at 22:34

3 Answers3

12

Download the binaries here according to your environment.

Project
+- glfw3.dll (You can put it in System32 or SysWOW64 instead.)
+- main.cpp

MinGW
+- include
|   +- GLFW
|      +- glfw3.h
+- lib
   +- libglfw3.a
   +- libglfw3dll.a (Renamed from glfw3dll.a)

g++ -c main.cpp
g++ -o main.exe main.o -lglfw3dll -lopengl32
chehsunliu
  • 1,559
  • 1
  • 12
  • 22
  • 8
    Have anybody prepackaged GLFW with MingW so we don't have to bother? If so this should increase the number of programmers who can try experimenting with OpenGL with about a magnitude. For god's sake, its 2014, and Windows still haven't come up with a standardized package installation mechansism that just works. – Nordlöw Sep 17 '14 at 22:36
  • 1
    2019.5 still no package for GLFW, please say I'm wrong – Kaiser Keister May 17 '19 at 03:53
  • 1
    pacman -S mingw-w64-glfw, msys2 command to use glfw with mingw64 – aeb-dev Jun 18 '19 at 12:06
  • In case it helps anyone else, following up on @MrMuMu's comment, I had to install the `mingw-w64-x86_64-glfw` package with `pacman -S mingw-w64-x86_64-glfw`, and compile with `g++ main.cpp -I/mingw64/include/ -L/mingw64/lib -lglfw3 -lopengl32` (though I've probably misconfigured my path somehow) – Christian Reall-Fluharty Jan 06 '21 at 09:04
  • @ChristianReall-Fluharty can i get some help – SHikha Mittal Jan 17 '21 at 07:34
0

I created makefile, it's for C, but you can edit it:

all:
    gcc -I ./include ./src/*.c -L ./lib -lglfw3dll -lopengl32 -lmingw32 -lgdi32 -luser32 -lkernel32 -lglew32 -o ./bin/main
openglfolder:.
│   

makefile

│
├───bin
│       glew32.dll
│       glfw3.dll
│       main.exe

│
├───include
│       eglew.h
│       glew.h
│       glfw3.h
│       glxew.h
│       wglew.h

│
├───lib
│       glew32.dll
│       glew32.lib
│       glew32s.lib
│       libglfw3.a
│       libglfw3dll.a

│
└───src
        main.c

edit : this works only for windows

Clifford
  • 88,407
  • 13
  • 85
  • 165
krystof
  • 1
  • 3
0

If you are using MinGW64 with MSYS2, install glfw library in your compiler. Open MSYS2 MINGW64 console and put:

pacman -S mingw-w64-x86_64-glfw

then compile your project using the following flags: -lglfw3 -lkernel32 -lopengl32 -lglu32

Valentina
  • 518
  • 7
  • 18