The process of creating a simple GLFW test program (Question)

arkathorn wrote on Friday, November 06, 2015:

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?

arkathorn wrote on Monday, December 07, 2015:

Nobody?

There’s documentation on compiling with GLFW which also links to a Beginner’s Guide to Linkers. Also this answer on Stackoverflow may help.

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.

mmozeiko wrote on Monday, December 07, 2015:

It should be lowercase -l for specifying libraries: -lglfw32 -lopengl32. Uppercase L is for specifying folders where to search libraries.

Thanks, apologies for mistake - corrected that.

arkathorn wrote on Wednesday, December 09, 2015:

I read that, but what files do I download? What do I do, with which files? The process in it’s entirety is not detailed.

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.

Good luck.

arkathorn wrote on Wednesday, December 09, 2015:

Welp. Still the same old undefined reference errors. Project dump follows:

File structure:

+- GLFW
|   +- glfw3.h
|   +- glfw3native.h
+- build.bat
+- glfw.dll
+- glfwdll.a
+- libglfw3.a
+- Main.cpp

build.bat:

g++ -o glfw-test -lglfw3 -lopengl32 main.cpp
pause

Main.cpp:
(See Above)

I apologize if this is really obvious, but I have no experience with manual compilation/linking.

Could you add the errors from the compile please.

arkathorn wrote on Wednesday, December 09, 2015:

Whoops, forgot about those.

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.

  1. 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
  2. I removed the iostream include and using namespace as you don’t need them for this.
  3. I compiled Main.cpp to Main.o using g++ -c -I. Main.cpp
  4. I linked using: g++ -o Main.exe Main.o -L. -lglfw3 -lopengl32 -lgdi32

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.

Hope this helps.

arkathorn wrote on Friday, December 11, 2015:

It worked! I cannot thank you enough for your help.

Sorry, I’ll try again. -L. -lglfw3 is equivalent to libglfw3.a, i.e. just specifying the library file name.