如何让OpenGL Hello World为OSX工作?

时间:2013-09-05 01:56:52

标签: c++ macos opengl

我一直在尝试在OSX Lion上编译Hello World OpenGL应用程序。我尝试使用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;
}

但我收到以下错误:

   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

1 个答案:

答案 0 :(得分:3)

Mac OS X是一个非常非标准的平台。您遇到过其中一种情况。特别是,您不是在OS X上链接GL和GLUT库,而是使用适当的框架:-framework OpenGL-framework GLUT而不是-lGL-lglut

在你的代码中,你应该有这样的东西:

#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

一般来说,您需要更多#ifdef __APPLE__黑客来编写适用于OS X和其他更标准平台的代码。但这应该涵盖OS X的大部分基础。另请注意,使用 glew 或类似库的任何内容都应在OS X上完全预处理;您(通常)不会在OS X上的运行时加载扩展。