I have recently begun with C++, and am attempting to begin OpenGL programming, starting with GLFW. I have created a small test program, to be built upon as I go:
#include <iostream>
#include <GLFW/glfw3.h>
using namespace std;
int main() {
if(!glfwInit()) {
return 1;
}
glfwTerminate();
return 0;
}
I am compiling using MinGW’s g++. Unfortunately, though I have tried extensively to do so, I cannot discern how to properly link GLFW to the compilation, in such a way as it can actually be compiled. Can anyone detail the process? In detail?
With g++ your command line should look something like:
g++ -o myexe mysourcefile.cpp -lglfw32 -lopengl32
If you are having problems getting this to work, then paste your command line used along with the output from the compiler and notes as to what files you have.
I disagree - the process in its entirety is very well detailed, but aimed at more experienced programmers than yourself. You didn’t mention that you did not know where to download the required files in your question - if you want to get a detailed response then please be specific.
See the glfw download page for download links. This is linked to from the main glfw page under the heading ‘download’. You need either the 32bit or 64bit Windows binaries depending on which version of MinGW you have. You’ll find the libraries in the folder lib-mingw(version).
I recommend moving the libraries into the same folder as your C++ source code for now, and move the GLFW folder which is in the Include directory into the directory you have your C++ code in. This means you don’t have to set up paths for your include and libraries.
C:\Users\AccountName\AppData\Local\Temp\cc14CDRC.o:Main.cpp:(.text+0xc): undefined reference to `glfwInit'
C:\Users\AccountName\AppData\Local\Temp\cc14CDRC.o:Main.cpp:(.text+0x21): undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status
OK - I also encountered these problems, but found a fix.
I’m using the 32bit version of MinGW. I can tell this as I see mingw32 in the output and not mingw64 (add -v to command line for verbose output from g++ if you don’t see which version your using). Thus I used the libs from glfw-3.1.2.bin.WIN32\lib-mingw
I removed the iostream include and using namespace as you don’t need them for this.
I compiled Main.cpp to Main.o using g++ -c -I. Main.cpp
Basically there seems to be a problem with compiling and linking on one command line, not sure why (it’s been 15 years since I regularily used unix and I’ve never used MinGW).
Note that I used -I. and -L. to set the compile and link directories as g++ didn’t default to the current dir as I expected.