Compiling OpenGL on Mac OS X or Linux
- January 16th, 2010
- Write comment
Most linux distributions make it easy to compile programs using OpenGL. Other systems, however, aren’t so easy. Here are instructions on how to get it running in both platforms.
Ubuntu / Linux
- Make sure you install the opengl-mesa packages for opengl development. On ubuntu you can get them all with the following invocation:
sudo apt-get install freeglut3-dev
- You should include the following #includes in your code referencing opengl:
# include <GL/gl.h> # include <GL/glu.h> # include <GL/glut.h>
- You should also have the following gcc cmdline flags in your makefile:
gcc -o simple simple.c -lGL -lglut -lGLU
Mac OS X (tested on Snow Leopard)
- For this to work you will have to install gcc and Xcode, which should install all the required opengl/glut libraries. You can get it here.
- Now, you’ll have to alter all the #includes in your files to conform to the proper location. Change all your opengl #includes to the following:
# include <OpenGL/gl.h> # include <OpenGL/glu.h> # include <GLUT/glut.h>
- Additionally your gcc command line will need to contain the following flags to link, along with any others you might have:
gcc -o simple simple.c -framework Carbon -framework OpenGL -framework GLUT
Cross Platform Compatibility
If you want your code to run on both, you need to get the Makefile and code to load the proper libraries depending on your system.
In all your OpenGL .c files, add the following header instead of your linux/mac includes:
#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
Then in your makefile, add the following before your build targets:
#default cmdline flags
LDFLAGS = -lGL -lglut -lGLU
# Mac OS alternate cmdline link options ifeq "$(OSTYPE)" "Darwin" LDFLAGS = -framework Carbon -framework OpenGL -framework GLUT endif