如何让 OpenGL Hello World 为 OSX 工作?
Posted
技术标签:
【中文标题】如何让 OpenGL Hello World 为 OSX 工作?【英文标题】:How to get an OpenGL Hello World working for OSX? 【发布时间】:2013-09-05 01:56:52 【问题描述】:我一直在尝试在 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
【问题讨论】:
请注意,即使已安装的 GL 库也无法加载其依赖项?它与项目无关。您应该安装的某些扩展程序已失效或丢失。 是的。不过,我不知道我应该如何解决这个问题。 ***.com/questions/10866331/freeglut-on-mac-os-x-lion 【参考方案1】: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__
hack 来编写可在 OS X 和其他更标准平台上运行的代码。但这应该涵盖 OS X 的大部分基础。还要注意,任何使用 glew 或类似库的东西都应该在 OS X 上完全预处理;您(通常)不会在 OS X 上运行时加载扩展。
【讨论】:
以上是关于如何让 OpenGL Hello World 为 OSX 工作?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Windows 中运行基本的“Hello World”GLSL 程序? [关闭]
3dMax建模笔记:介绍3dMax和创建第一个模型Hello world