2

I've been trying to compile a Hello World OpenGL application on OSX Lion. I have tried compilling the following file with g++ main.cpp -o test -lglut

#include <GL/glut.h>

void displayMe(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.5, 0.0, 0.0);
        glVertex3f(0.5, 0.5, 0.0);
        glVertex3f(0.0, 0.5, 0.0);
    glEnd();
    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(300, 300);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Hello world :D");
    glutDisplayFunc(displayMe);
    glutMainLoop();
    return 0;
}

But I get the following errors:

   In file included from /usr/local/include/GL/glut.h:17,
                 from main.cpp:1:
/usr/local/include/GL/freeglut_std.h:122:19: error: GL/gl.h: No such file or directory
/usr/local/include/GL/freeglut_std.h:123:20: error: GL/glu.h: No such file or directory
In file included from /usr/local/include/GL/glut.h:17,
                 from main.cpp:1:
/usr/local/include/GL/freeglut_std.h:439: error: variable or field ‘glutUseLayer’ declared void
/usr/local/include/GL/freeglut_std.h:439: error: ‘GLenum’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:498: error: ‘GLenum’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:499: error: ‘GLenum’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:501: error: ‘GLenum’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:516: error: variable or field ‘glutWireCube’ declared void
/usr/local/include/GL/freeglut_std.h:516: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:517: error: variable or field ‘glutSolidCube’ declared void
/usr/local/include/GL/freeglut_std.h:517: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:518: error: variable or field ‘glutWireSphere’ declared void
/usr/local/include/GL/freeglut_std.h:518: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:518: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:518: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:519: error: variable or field ‘glutSolidSphere’ declared void
/usr/local/include/GL/freeglut_std.h:519: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:519: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:519: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:520: error: variable or field ‘glutWireCone’ declared void
/usr/local/include/GL/freeglut_std.h:520: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:520: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:520: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:520: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:521: error: variable or field ‘glutSolidCone’ declared void
/usr/local/include/GL/freeglut_std.h:521: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:521: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:521: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:521: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:523: error: variable or field ‘glutWireTorus’ declared void
/usr/local/include/GL/freeglut_std.h:523: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:523: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:523: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:523: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:524: error: variable or field ‘glutSolidTorus’ declared void
/usr/local/include/GL/freeglut_std.h:524: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:524: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:524: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:524: error: ‘GLint’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:537: error: variable or field ‘glutWireTeapot’ declared void
/usr/local/include/GL/freeglut_std.h:537: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:538: error: variable or field ‘glutSolidTeapot’ declared void
/usr/local/include/GL/freeglut_std.h:538: error: ‘GLdouble’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:546: error: ‘GLenum’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:551: error: ‘GLenum’ was not declared in this scope
/usr/local/include/GL/freeglut_std.h:560: error: ‘GLfloat’ has not been declared
/usr/local/include/GL/freeglut_std.h:560: error: ‘GLfloat’ has not been declared
/usr/local/include/GL/freeglut_std.h:560: error: ‘GLfloat’ has not been declared
/usr/local/include/GL/freeglut_std.h:561: error: ‘GLfloat’ does not name a type
main.cpp: In function ‘void displayMe()’:
main.cpp:5: error: ‘GL_COLOR_BUFFER_BIT’ was not declared in this scope
main.cpp:5: error: ‘glClear’ was not declared in this scope
main.cpp:6: error: ‘GL_POLYGON’ was not declared in this scope
main.cpp:6: error: ‘glBegin’ was not declared in this scope
main.cpp:7: error: ‘glVertex3f’ was not declared in this scope
main.cpp:11: error: ‘glEnd’ was not declared in this scope
main.cpp:12: error: ‘glFlush’ was not declared in this scope
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286

1 Answers1

3

Mac OS X is a very non-standard platform. You have encountered one of those situations. In particular, instead of linking against the GL and GLUT libraries on OS X, you use the appropriate framework: -framework OpenGL and -framework GLUT instead of -lGL and -lglut

In your code, you should have something like this:

#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

Generally speaking you'll need a lot more #ifdef __APPLE__ hacks to write code that works on OS X and other more standard platforms. But this should cover most of the bases for OS X. Also note that anything that uses glew or libraries like it should be completely pre-processed out on OS X; you (generally) don't load extensions at run-time on OS X.

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106