Installing OpenGL with GLUT

You need to install an environment on your hardware where you can compile and run OpenGL programs. OpenGL is supported on most modern operating systems. Linux and OSX natively supports OpenGL, but on Windows you need to use GLEW to get a recent version of OpenGL.

Before asking for help, look at the Troubleshooting hints to localize the problem rather than asking a meaningless question like It doesn't work.

My examples use GLUT is the simplest environment for OpenGL, except for examples glfw21, sdl21v1 and sdl21v2 which use OpenGL with GLFW, SDL version 1.2 and SDL version 2, respectively.

If you want to use GLFW instead of GLUT for your programs, see Installing OpenGL with GLFW.

Linux

OpenGL support is very easy to enable on current Linux distributions. For distributions derived from RedHat Linux, the libraries and header files are installed using the command
yum install freeglut-devel
Since GLUT depends on OpenGL and a number of other libraries, installing GLUT will trigger the dependencies needed to install everything else. For distributions derived from Debian such as Ubuntu, the installation command is
apt-get install freeglut3-dev
To compile and link your program on Ubuntu based distros you need to explicitly grab every library using
gcc -o foo foo.c -lglut -lGLU -lGL -lm

Once installed, run the glxinfo program and look for direct rendering in the output. If the result is YES, then hardware support for OpenGL is working. If it is NO, some things are done in software and you may take a performance hit. Depending on your hardware, you may want to work on your X server. Specifically, the nVidia and AMD/ATI web sites contains updated drivers that result in improved performance over the stock Xorg drivers.

On Ubuntu using the Additional Drivers integrates the vendor drivers with the Ubuntu kernels, which is the recommended procedure to void manual updates when the kernel is upgraded. Be careful to not override the Ubuntu mechanism to maintain the kernel and drivers, as this can leave the system in an unbootable state or break the X server.

The compiz window manager (which is an OpenGL window manager) makes applications with high frame rates run jerky unless you enable VSync. This seems to be an issue especially with newer Ubuntu installs.

OS/X

The OS/X Darwin environment is transitioning away from OpenGL. Apple is pushing their propietary Metal environment for graphics, but still supports OpenGL although strictly it has been deprecated. Apple makes it hard to use OpenGL features beyond OpenGL 3.2. OpenGL 4 is supported on Apple hardware but only using the core profile so switching between the fixed and programmable pipelines is not supported.

Start by updating Xcode to the latest release. This is a multi-gigabyte download. When installing Xcode be sure to select Command Line Tools as part of the install.

To compile and link your program using the Apple SDK requires

gcc -o foo foo.c -framework GLUT -framework OpenGL
Note that under OS/X, the GLUT header files are in the subdirectory GLUT rather than the GL subdirectory. The following code works on OSX and Linux
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
Apple considers the OpenGL API deprecated and will throw LOTS of warnings if you do not use the -Wno-deprecated-declarations compiler flag.

Windows: MSYS2/MinGW

Various Windows versions support OpenGL natively, but graphics card manufacturers often replace the native Windows OpenGL libraries with their own libraries that support specific features of their hardware.

Since most Windows environments do not contain a native compilation suite, installing the necessary header files and libraries differs depending on the compiler used.

Your simplest option to get a full toolchain on Windows is to use the MSYS2 platform, which provides an easy to use environment for installing and running native Windows software. MSYS2 uses the MinGW toolchain and the pacman package manager to easily install various packages.

Start by downloading and installing MSYS2 from here. Just use the default locations. Once installed launch MSYS and update the packages to the latests version using.

pacman -Syu
This may close the window in some cases. Relaunch the window and rerun the command until it says everything is up to date.

Now install the compiler toolchain using

pacman -S --needed base-devel mingw-w64-x86_64-toolchain
Just do all which installs way more than we need but that's OK.

Now launch MSYS MinGW 64-bit from the Start window. You can test the compiler with the hello world rogram to make sure all is working.

Now install GLUT and GLEW. It is as simple as doing

pacman -S mingw-w64-x86_64-freeglut
pacman -S mingw-w64-x86_64-glew
You can install any other package you want exacly the same way. To compile a program using GLUT and GLEW do
gcc -Wall -o foo foo.c -lfreeglut -lglew32 -lglu32 -lopengl32 -lm
Note that in my example programs, using GLEW is required on some systems and not on others. So I use the compiler flag -DUSEGLEW to conditionally compile in GLEW when needed. My system does not require or support GLEW, so if you do not make using GLEW conditional the build will fail on my hardware.

Troubleshooting hints

If you are having trouble getting the programs to compile, you may want to consider these things to determine if the problem is in your compiler, header files or linker/libraries.