Installing OpenGL with GLFW

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 natively supports the latest OpenGL, but on OSX and Windows you need to use GLEW to get at the latest 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.

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 glfw-devel
Since GLFW depends on OpenGL and a number of other libraries, installing GLFW will trigger the dependencies needed to install everything else. For distributions derived from Debian such as Ubuntu, the installation command is
apt-get install libglfw3-dev
To compile and link your program on Ubuntu based distros you need to explicitly grab every library using
gcc -o foo foo.c -lglfw3 -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 makes it difficult to fully utilize the hardware when not using their programming suites.

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.

Next download and install Cmake for OSX from here. Get the latest .dmg and install it. Cmake is required to install GLFW and is not provided by OSX by default. If you already have a working cmake installation from another souce that should be fine.

Now download GLFW for here and place the 64-bit macOS binaries somewhere convenient. Navigate to the download location (e.g. Downloads/glfw-3.3.2) and run the following commands:

mkdir build
cd build
/Applications/Cmake.app/Contents/bin/cmake ..
make
sudo make install
This builds and installs GLFW into /usr/local. You can now compile and link GLFW based programs using
gcc -Wall -o foo foo.c  -lglfw3 -framework Cocoa -framework OpenGL -framework IOKit
Note the need for the Cocoa and IOKit frameworks since GLFW relies on these. Apple places the OpenGL files in a different location than other systems, so it is important that you make the header file inclusion conditional
#ifdef __APPLE__
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>
#else
#include <GL/glu.h>
#include <GL/gl.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.

Apple support for OpenGL 3.3 and later is necessary to use GLEW. Download the GLEW source code from here. Navigate to the downloaded location (e.g. Downloads/glew-2.1.0) and do

make
sudo make install
This builds and installs GLEW into /usr/local. You can now compile and link programs using
gcc -Wall -o foo foo.c  -lglfw3 -lglew -framework Cocoa -framework OpenGL -framework IOKit
To access features beyond OpenGL 3.2 on OSX, you need to provide the following hints to GLFW. Note that this only allows access to the core profile, not the compatibility profile, which is much stricter about what you can do in OpenGL 4.
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,2); 
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
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. I use the -DAPPLE_GL4 flag to include window hints shown above.

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 GLFW and GLEW. It is as simple as doing

pacman -S mingw-w64-x86_64-glfw
pacman -S mingw-w64-x86_64-glew
You can install any other package you want exacly the same way. To compile a program using GLFW and GLEW do
gcc -Wall -o foo foo.c -lglfw3 -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.

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.