4
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 Compiling OpenGL programs on Windows, Linux and OS X http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/2009/compiling.html 1 of 4 5/19/2010 9:10 AM

Compiling OpenGL Programs o

Embed Size (px)

Citation preview

Page 1: Compiling OpenGL Programs o

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 thecygwin 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 torecompile 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

Compiling OpenGL programs on Windows, Linux and OS X http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/2009/compiling.html

1 of 4 5/19/2010 9:10 AM

Page 2: Compiling OpenGL Programs o

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 tohave 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 = assignment1LDFLAGS = -lGL -lGLU -lglut

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

# OS Xifeq "$(OSTYPE)" "darwin" LDFLAGS = -framework Carbon -framework OpenGL -framework GLUTendif

$(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 youneed 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 havingtrouble finding a dependency, try Googling for compile opengl <your distro>, or post a message to the

Compiling OpenGL programs on Windows, Linux and OS X http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/2009/compiling.html

2 of 4 5/19/2010 9:10 AM

Page 3: Compiling OpenGL Programs o

I3D newsgroup.

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

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):

gccgtk2-x11-develpkg-configmakefreeglut

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 whenit comes to submitting their assignments. Remember that if your assignment doesn't compile and run in theSutherland lab, you will likely receive zero marks.

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

These instructions apply to

The free VC++ 2008 Express Edition described aboveThe commercial VC++ 2008 editionOlder versions of Visual C++ (down to version 6), with slight variations in the layout of the dialogs andmenus.

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

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

Compiling OpenGL programs on Windows, Linux and OS X http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/2009/compiling.html

3 of 4 5/19/2010 9:10 AM

Page 4: Compiling OpenGL Programs o

Click on tools and go to options1.Expand "Projects and Solutions" and click on "VC++ Directories"2.Click on the "Show Directories For" dropdown list and click on "Include files"3.Click the "New Line" icon4.In the new entry that appears, type or paste the location of where you have extracted the GLUT filesand hit Enter.

5.

Repeat steps 3 through 5 for "Library files".6.

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 whenit comes to submitting their assignments. Remember that if your assignment doesn't compile and run in theSutherland lab, you will likely receive zero marks.

Compiling OpenGL programs on Windows, Linux and OS X http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/2009/compiling.html

4 of 4 5/19/2010 9:10 AM