I was having a problem using Glut (GL Utility Toolkit) on my Mac today – despite the number of sites saying that it was a snap to set up: ”Just add the GLUT.Framework to your project and compile.” – Well, my program was compiling OK, but running it would always result in a “bus error” and drop into the debugger. I tried various combinations of compiling from command line, running under X11, trying port and fink for different libraries – What could this weird problem be?
I eventually came across the root of the problem and since I haven’t seen it posted anywhere on the web I decided to add my own contribution to the “GLUT on OS X” billboard. I’ll try to give the complete setup I used to start a Glut project in XCode.
Setup XCode Project
Like many others will suggest, open XCode (or project builder) and create a Standard Tool (or C++ Application or whatever). I recommend a Standard Tool because you just get a basic main.c file and that’s enough. Add the GL.Framework and GLUT.Framework files from /System/Library/Frameworks. (This is where they are on my Mac)
Rename the file main.cpp/m to main.c for a basic C application. (I’ll stick with this for now as it is sufficient)
Modifying the standard code
Headers need to be changed from gl/ to OpenGL/ and GLUT/ in order to correctly locate the headers in OS X’s frameworks. The top of main.c should read as follows:
-
#include <OpenGL/gl.h>
-
#include <OpenGL/glu.h>
-
#include <GLUT/glut.h>
And here’s the bit that was killing me. Most of my glut code is based on the skeleton provided from NeHe’s tutorials which initialise glut then initialise opengl then sets up glut’s window properties. like so:
-
int main ( int argc, char** argv ){
-
glutInit ( &argc, argv );
-
initGL ( );
-
glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE );
-
glutInitWindowSize ( 500, 500 );
-
glutCreateWindow ( "NeHe’s OpenGL Framework" );
-
glutDisplayFunc ( display );
-
glutReshapeFunc ( reshape );
-
glutKeyboardFunc ( keyboard );
-
glutSpecialFunc ( arrow_keys );
-
glutMainLoop ( );
-
return 0;
-
}
OK, everything looks good right? Not for OS X apparently. Maybe this is obvious for some people but I had to trace a bit to see what was causing my problem. As it turns out I was able to fix it by moving the initGL call after all the glutInit*/glutCreate* calls. Thus, my new main looks as follows:
-
int main ( int argc, char** argv ){
-
glutInit ( &argc, argv );
-
glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE );
-
glutInitWindowSize ( 500, 500 );
-
glutCreateWindow ( "NeHe’s OpenGL Framework" );
-
initGL ( );
-
glutDisplayFunc ( display );
-
glutReshapeFunc ( reshape );
-
glutKeyboardFunc ( keyboard );
-
glutSpecialFunc ( arrow_keys );
-
glutMainLoop ( );
-
return 0;
-
}
I hope this is helpful to someone else since it seems to be a rather obscure error (that only affects Mac OS?). I’m glad I caught it though – as much as I intend to use Cocoa in the future, it’s nice to know a simple framework is still available.
Programming
error, glut, help, mac, opengl, Programming