Introduction
OpenGL is a popular and widely implemented graphics specification, and is often a good choice when you need to write programs featuring 2D or 3D graphics. However, OpenGL defines how graphics should be drawn, and it is up to the programmer to handle things such as window definition and input from the mouse and keyboard. This can be performed using API specific to the windowing system, which of course requires that the programmer have a good knowledge of the API. Alternatively, libraries such as GLUT or SDL may be used. These provide an abstraction layer from the windowing system, with the added advantage that programs may be written which are independent of architecture and operating system (assuming of course that appropriate development tools and libraries are available on that platform).
This tutorial focuses on GLUT, and explains how to develop OpenGL applications for Windows using the C or C++ languages with MinGW (Minimalist GNU For Windows). MinGW was chosen because it is freely available for download, and because its usage will be familiar to anyone who has used the GCC.
I will cover the setting up of both freeglut and GLUT for Win32 in this tutorial, as these are the two most widely used versions of GLUT (there is OpenGLUT too, but this isn’t actively developed). My personal preference is to use freeglut, but you may have good reason to use GLUT for Win32 instead. One thing I would like to point out, is that my GLUT for Win32 import libraries produce executables which are binary compatible with the DLL from the original author’s website. For freeglut, the author does not distribute a Windows DLL, so I have compiled my own version using MinGW. There is no guarantee that this freeglut DLL will be binary compatible with a freeglut DLL built with another compiler, because each compiler has its own naming convention for exported stdcall functions (in the case of Microsoft Visual Studio, two different conventions depending on whether you use a module definition or the “__stdcall” decoration!).
Setting Up MinGW
If you haven’t already done so, make sure you have the latest stable versions of the MinGW packages installed. These may be obtained from the SourceForge project page. You’ll need the “gcc-core”, “binutils”, “MinGW Runtime”, and “w32api” packages to develop GLUT applications (these were probably installed by default if you used the MinGW setup program). To develop C++ applications, you’ll also need “gcc-c++”.
Setting Up freeglut With MinGW
An alternative to using the original GLUT libraries is offered by freeglut. This is a more up to date open source implementation of GLUT, and features some extensions too. I’ve created a freeglut MinGW package (with checksums), which contains import libraries, headers, and a Windows DLL. Using this, you can either dynamically link against the freeglut DLL, or statically link the library into your application.
Once you have downloaded this package, copy “libfreeglut.a” and “libfreeglut_static.a” from the “lib\” directory of the package to the “lib\” directory of your MinGW installation. Copy all of the header files from the “include\” directory of the package to the “include\GL” directory of your MinGW installation (be careful if you have another GLUT installation, as it will likely also have placed a “glut.h” in this folder—you should back up the original rather than overwrite it).
To run freeglut applications, you will need to copy “freeglut.dll” to the same directory as the application, or install it system‑wide in a location such as “C:\Windows\System32\” on a typical Windows installation (be careful when placing DLLs in system‑wide locations like this, as you could end up breaking applications which were compiled against an incompatible version).
Compiling freeglut Applications With MinGW
To keep your applications compatible with GLUT, you should “#include <GL/glut.h>”. If you want to use freeglut specific extensions, you can “#include <GL/freeglut.h>” instead, but bear in mind that the resulting code can’t be compiled against other GLUT implementations without modification.
To dynamically link against freeglut, you should include the linker flags “-lfreeglut -lopengl32”. To have the executable run as a Windows GUI application rather than a console application, you need to add “-Wl,--subsystem,windows” to the command line as well:
gcc -o progname.exe sourcefile.c -lfreeglut -lopengl32 -Wl,--subsystem,windows
To statically link against freeglut, you need to “#define FREEGLUT_STATIC”, which is best done by adding “-D FREEGLUT_STATIC” to the command line. You also need to link against the static freeglut library as well as some additional Windows libraries with the linker flags “-lfreeglut_static -lopengl32 -lwinmm -lgdi32” (n.b. the change from “-lfreeglut” to “-lfreeglut_static”):
gcc -o progname.exe sourcefile.c -D FREEGLUT_STATIC -lfreeglut_static -lopengl32 ^
-lwinmm -lgdi32 -Wl,--subsystem,windows
If you get undefined references to functions when trying to statically link against freeglut, check your macro definitions and linker flags—static linking will fail if you forget to define the macro “FREEGLUT_STATIC”, or if you have defined this macro but are linking against the dynamic library.
In either case, if you need to use GLU functions, you should add “-lglu32” to the command line too.
Setting Up “GLUT for Win32” With MinGW
If you want to compile applications using GLUT for Win32 instead of freeglut, you just need to have import libraries which are compatible with MinGW. I was unable to find a suitable up to date package on the internet, so I have created a GLUT import libraries for MinGW (with checksums).
The MinGW “w32api” package actually comes with some older and incompatible GLUT import libraries. These may be found in the “lib\” directory of your MinGW installation under the names of “libglut.a” and “libglut32.a”. Before you do anything else, you should remove them from the directory and put them somewhere safe (in case you decide to reinstate them at a later date). Having done this, you can copy the new “libglut32.a” from my package, and place it in the “lib\” folder. Also bear in mind, that when you update your “w32api” package, it could overwrite “libglut32.a” with the old version. If it does, simply repeat this stage. The “glut.h” file can then be placed into the “include\GL\” directory of your MinGW installation.
To run GLUT applications, you will need a copy of “glut32.dll”. You can get this from Nate Robins’ site (the “glut-3.7.6-bin.zip” package).
Compiling GLUT Applications With MinGW
To use functions from the GLUT for Win32 library, you should “#include <GL/glut.h>” in your source code. When linking a GLUT application, you should link against the GLUT for Win32 and OpenGL libraries, with the flags “-lglut32 -lopengl32”. Adding the flag “-Wl,--subsystem,windows” will ensure it compiles as a Windows GUI application rather than a console application. Thus the command line to compile a typical GLUT application would be:
gcc -o progname.exe sourcefile.c -lglut32 -lopengl32 -Wl,--subsystem,windows
![Compiling a GLUT application [Windows XP cmd.exe: gcc -o progname.exe sourcefile.c -lglut32 -lopengl32 -Wl,--subsystem,windows]](/common/images/glut-mingw-compile.png)
Again, if you need to use GLU functions, you should add “-lglu32” to the command line.
Example GLUT Application
Here is very basic example GLUT application written in C, which you can use for testing once you have configured GLUT.
#include <stdlib.h>
#include <GL/glut.h>
void keyboard(unsigned char key, int x, int y);
void display(void);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutCreateWindow("GLUT Test");
glutKeyboardFunc(&keyboard);
glutDisplayFunc(&display);
glutMainLoop();
return EXIT_SUCCESS;
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '\x1B':
exit(EXIT_SUCCESS);
break;
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush();
}
Running the application should produce something very similar to this:
![GLUT example program [Image of a window contining a red square in the middle]](/common/images/glut-example.png)
Cross‑Compiling GLUT Applications
Although this tutorial focused on compiling GLUT applications with the Windows version of MinGW, you can of course cross‑compile them on non‑Windows versions. You can use the same packages as used in this tutorial, and follow the same instructions.