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
4

1 回答 1

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 上运行时加载扩展。

于 2013-09-05T02:06:22.600 回答