Get OpenGL running with MinGW on Windows 10

This article describes the steps for getting everything configured and running to build OpenGL applications on a Windows 10 machine (as of the 19th of August, 2016) using gcc from MinGW with the required GLUT and GLEW libraries. One can get pre-built binaries for linking Visual Studio applications for GLUT and GLEW. This article is about using MinGW to build applications on Windows.

 

One of the main purposes of this article was to create a Linux like environment to work with OpenGL in Windows, and to not depend on Visual Studio on development and to use the same code and build command as used on Linux on Windows.

 

Prerequisites -

 

Step 1: Install MinGW


Install MinGW to the default location of C:\MinGW with gcc, g++ and make.exe (MSYS). Then add C:\MinGW\bin to the System path by adding it to the PATH environment variable.

 

Step 2: Install freeglut


Download freeglut built for mingw from here.

After downloading the files, copy the include, lib and bin folders to the corresponding include, lib and bin folders of MinGW.

 

Step 3: Install GLEW


Download GLEW from here.

Use the following steps to compile GLEW from MinGW.

After building GLEW for MinGW, copy the include, lib and bin folders to the corresponding include, lib and bin folders of MinGW.

 

Step 4: Write test code


  

#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>

void reshape(int, int);
void display(void);
void keyboard(unsigned char, int, int);

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    
    return 0;
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
    glMatrixMode(GL_MODELVIEW);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    
    glLoadIdentity();
    gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glScalef(1.0, 2.0, 1.0);
    glutWireCube(1.0);
    glFlush();
}

void keyboard(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 27: exit(10);
                 break;
    }
}

 

Step 5: Compile the code and run it.


 

gcc -o opengl_test.exe gl_test.cpp -lfreeglut -lopengl32 -lglu32

The same code runs on Linux (Ubuntu) using -

gcc gl_test.cpp -o opengl_test -lGL -lGLU -lglut