Setting up GLFW with MinGW
Asked Answered
S

3

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?

Sherburn answered 14/10, 2012 at 21:16 Comment(7)
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.Testis
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.Sherburn
codeblocks uses MinGW under Windows, so i suppose that you can fix your problems just reading this https://mcmap.net/q/1165142/-glfw-and-codeblocksTestis
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.Sherburn
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.Testis
I've tried a hundred solutions now and gave up. I'm going to try SFML now.Sherburn
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 fixNilgai
R
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
Rathe answered 20/6, 2013 at 16:40 Comment(5)
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.Gatian
2019.5 still no package for GLFW, please say I'm wrongMoguel
pacman -S mingw-w64-glfw, msys2 command to use glfw with mingw64Refreshment
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)Upset
@ChristianReall-Fluharty can i get some helpIrremediable
B
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

Backbencher answered 9/3, 2021 at 21:40 Comment(0)
C
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

Cozmo answered 12/6, 2023 at 2:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.