Compiling OpenGL programs on Windows, Linux and OS X


Here's a simple OpenGL program that does nothing:

#include <GL/gl.h>
#include <GL/glut.h>

void display()

int main(int argc, char **argv)
    glutInit(&argc, argv);

Assuming this program is saved in hello.c, you can compile it by typing:

gcc -o hello hello.c -lGL -lGLU -lglut

On Windows, if you have GLUT and cygwin installed, the same program will compile by typing (in the cygwin shell):

gcc -o hello.exe hello.c -lopengl32 -lglut32

On Mac OS X, you need to change the #include directives slightly to:

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

and compile it by typing (in a terminal window):

gcc -o hello hello.c -framework Carbon -framework OpenGL -framework GLUT

Using a makefile

You are required to use a makefile to compile your program for all three assignments. This makes it easy to recompile and test your program without typing the long gcc command above.

A makefile is always called Makefile (with no extension). A simple makefile for Linux is:

LIBS = -lGL -lGLU -lglut

hello : hello.c
    gcc -o $@ $< $LIBS

You must use a tab to indent the gcc line (spaces will not work).

Writing for Windows, OS X and Linux

It can be troublesome rewriting your program every time you switch computers. Follow these instructions to have your programs and makefiles work on any computer, regardless of what operating system it's running.

Use the following include directives:

#ifdef __APPLE__
#  include <OpenGL/gl.h>
#  include <OpenGL/glu.h>
#  include <GLUT/glut.h>
#  include <GL/gl.h>
#  include <GL/glu.h>
#  include <GL/glut.h>

Use the following Makefile:

# Linux (default)
EXE = assignment1
LDFLAGS = -lGL -lGLU -lglut

# Windows (cygwin)
ifeq "$(OS)" "Windows_NT"
    EXE = assignment1.exe
        LDFLAGS = -lopengl32 -lglu32 -lglut32

# OS X
ifeq "$(OSTYPE)" "darwin"
        LDFLAGS = -framework Carbon -framework OpenGL -framework GLUT

$(EXE) : assignment1.c
        gcc -o $@ $< $(CFLAGS) $(LDFLAGS)

Remember to change the name of "assignment1" as appropriate.

Compiling at home on OS X

Install the developer tools (they are included amongst the installation DVDs). This includes everything you need so that the Makefile above will work fine.

Compiling at home on Linux

Some Linux distributions (e.g., Ubuntu) require you to install special -dev packages alongside the main ones. For example, to get the OpenGL header files, you might need to look for an opengl-dev package.

There are too many distributions around for us to give you any more guidance than this. If you are having trouble finding a dependency, try Googling for compile opengl <your distro>, or post a message to the I3D newsgroup.

Compiling in Cygwin

Cygwin is a Unix-like environment that runs on Windows. You can download it for free from the Cygwin website.

Run the Cygwin setup program to install or add additional Cygwin packages. To compile OpenGL programs, you'll need at least these packages (you might need to change the "View" from "Categorised" to "Alphabetical" so you can find them easily):

There many other useful packages, for example

To compile an OpenGL program in Cygwin, type (in the Cygwin shell):

gcc -o hello.exe hello.c -lopengl32 -lglu32 -lglut32

Due to a bug in Cygwin you must make sure the linker flags (-l...) are the last arguments given.

You must be careful to keep a UNIX Makefile up-to-date and check your work in Sutherland regularly. Students who develop solely on Windows often find that compatibility problems cause major headaches when it comes to submitting their assignments. Remember that if your assignment doesn't compile and run in the Sutherland lab, you will likely receive zero marks.

Compiling in Visual C++ 2008

Microsoft provide a free compiler and IDE for Windows called Visual C++ 2008 Express Edition. You can download it from the VS Express site (it's a big download).

These instructions apply to

You'll need to download the GLUT binaries for Windows. Extract the contents of this ZIP file anywhere, and remember the location of the header files (.h) and library files (.lib) for later. Place the .dll file in your windows\system32 directory.

In Visual C++, add the location of the GLUT files by following these steps:

  1. Click on tools and go to options
  2. Expand "Projects and Solutions" and click on "VC++ Directories"
  3. Click on the "Show Directories For" dropdown list and click on "Include files"
  4. Click the "New Line" icon
  5. In the new entry that appears, type or paste the location of where you have extracted the GLUT files and hit Enter.
  6. Repeat steps 3 through 5 for "Library files".

Create a "Console" application when you start a new project.