You are on page 1of 4

Compiling OpenGL programs on Windows, Linux and OS X

http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/2009/compiling.html

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);
glutDisplayFunc(display);
glutMainLoop();
}

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

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

1 of 4

5/19/2010 9:10 AM

Compiling OpenGL programs on Windows, Linux and OS X

2 of 4

http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/2009/compiling.html

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

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>
#else
# include <GL/gl.h>
# include <GL/glu.h>
# include <GL/glut.h>
#endif

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
endif
# OS X
ifeq "$(OSTYPE)" "darwin"
LDFLAGS = -framework Carbon -framework OpenGL -framework GLUT
endif
$(EXE) : assignment1.c
gcc -o $@ $< $(CFLAGS) $(LDFLAGS)

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

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

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

5/19/2010 9:10 AM

Compiling OpenGL programs on Windows, Linux and OS X

3 of 4

http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/2009/compiling.html

I3D newsgroup.

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):
gcc
gtk2-x11-devel
pkg-config
make
freeglut
There many other useful packages, for example
tcsh (if you prefer the CSH shell, used at RMIT, over BASH)
gdb (for debugging)
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.

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
The free VC++ 2008 Express Edition described above
The commercial VC++ 2008 edition
Older versions of Visual C++ (down to version 6), with slight variations in the layout of the dialogs and
menus.
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:

5/19/2010 9:10 AM

Compiling OpenGL programs on Windows, Linux and OS X

4 of 4

http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/2009/compiling.html

1.
2.
3.
4.
5.

Click on tools and go to options


Expand "Projects and Solutions" and click on "VC++ Directories"
Click on the "Show Directories For" dropdown list and click on "Include files"
Click the "New Line" icon
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.
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.

5/19/2010 9:10 AM

You might also like