MinGW Freeglut 64bit on Windows Example

MinGW is a port of GCC to Windows, many people use Visual Studio C++ IDE on Windows, but it's not freeware and it's huge and complex, sometimes a lightweight development environment is the thing you want. Download a MinGW distribution and unpack it to local, you can find a distribution here: https://nuwen.net/mingw.html.

Go to the unpacked folder and double click the open_distro_window.bat and type g++ --version, you are good to go.

 
 
E:\sync\MinGW>g++ --version
g++ (GCC) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 

Then we need to download glut, in this example I will use freeglut, the old glut is not maintained anymore. You can find a freeglut package for Windows at http://www.transmissionzero.co.uk/software/freeglut-devel/. It contains the GL header files and lib, dll files, both for 32bit and 64bit, use anyone suits you.

Create a C source file with the following content

 
#include <windows.h>
#include "GL/glut.h"
 
void Display(void)
 {
 glClear(GL_COLOR_BUFFER_BIT);
 
 glBegin(GL_TRIANGLES);
  glColor3f(1,1,1);
  glVertex3f(0,0,0);
  glVertex3f(-1,0,0);
  glVertex3f(0,1,0);
 
  glColor3f(0,1,0);
  glVertex3f(0,0,0);
  glVertex3f(1,0,0);
  glVertex3f(0,-1,0);
 glEnd();
 
 glFlush();
 }
 
int main(int argc, char** argv)
 {
 glutInit(&argc, argv);
 glutCreateWindow("MY Triangle");
 glutDisplayFunc(Display);
 glutMainLoop();
 return 0;
 }
 
 

And then copy three files to the same folder: libfreeglut.a, libfreeglut_static.a, freeglut.dll. When you compile the source file, you need the lib files, and when you run the executable file, you need dll file, you can find them all in the freeglut package. You also need to copy the freeglut-MinGW-3.0.0-1.mp.zip\freeglut\include\GL to MinGW\include\GL, it contains all freeglut header files.

To compile the source file to executable, type the command below in your MinGW cmd window:

 
g++ -o gltriangle.exe gltriangle.c -lopengl32 -lfreeglut -L"."
 

The output will be: