Mac OpenGL踩坑记

在Mac上尝试写OpenGL程序,结果踩了一堆坑,在此记录下来,以备后用。

Mac上已经带了GLUT,使用时只需#include <GLUT/GLUT.h>即可。但使用GLUT函数编译时,Xcode会警告相关函数已经在OS X 10.9中废弃:

‘glutInit’ is deprecated: first deprecated in OS X 10.9

消除该警告的方法是在Build Settings中将OS X Deployment Target改为OS X 10.8,但这显然不是Apple认可的解决方案,更好的方案是使用GLFW和GLEW替代GLUT。

http://stackoverflow.com/questions/24095931/glut-deprecation-in-mac-osx-10-9-ide-qt-creator

使用GLFW和GLEW的问题是OS X默认并没有带上这两个库,需要我们自己安装。

从GLFW官网上下载源码,使用CMake编译:

http://www.glfw.org/docs/latest/compile.html#compile_compile

编译出的静态库路径在用CMake新建的工程文件夹下:

glfw-3.1.2/src/Debug/libglfw3.a

#include头文件路径在源码文件夹下:

glfw-3.1.2/include/GLFW/glfw3.h

在Build Settings中设置好Header Search Paths和Library Search Paths,在Build Phases的Link Binary With Libraries将libglfw2.a设置好,此时编译Xcode会报很多Undefined symbols的错误:

Undefined symbols for architecture x86_64:

“_CFArrayAppendValue”, referenced from:

__glfwInitJoysticks in libglfw3.a(iokit_joystick.o)

_addJoystickElement in libglfw3.a(iokit_joystick.o)

这是因为libglfw3.a在Mac OS X上使用了Cocoa,依赖了几个其它的库:Cocoa、IOKit和CoreVideo。将这3个库也在Link Binary With Libraries中设置好,编译就能通过了。

http://stackoverflow.com/questions/18391487/compiling-with-glfw3-linker-errors-undefined-reference

使用GLEW先从它的官网上下载源码,然后依次使用3个make命令编译安装:

1、make,编译。

2、sudo make install,安装,将头文件和库文件拷贝到系统头文件和库文件存放目录。

3、make clean,清除编译安装过程中留下来的垃圾文件。

https://github.com/nigels-com/glew

在Mac OS X EI Capitan上sudo make install时遇到了以下问题:

install -d -m 0755 “/usr/include/GL”

install: mkdir /usr/include: Operation not permitted

make: *** [install.include] Error 71

这是因为从EI Capitan开始,Mac OS X启用了System Integrity Protection,不再允许直接操作/usr下的目录,但/usr/local除外,所以需要将GLEW安装在/usr/local下。这里只需要修改GLEW的Makefile即可,将GLEW_DEST ?= /usr修改为GLEW_DEST ?= /usr/local,再执行sudo make install。

http://stackoverflow.com/questions/33309005/sudo-mkdir-in-usr-operation-not-permitted-el-capitan

同样需要配置好Header Search Paths、Library Search Paths和Link Binary With Libraries。此时编译报错:

/usr/local/include/GL/glew.h:16426:17: Declaration of ‘PFNGLCOPYTEXSUBIMAGE3DPROC’ must be imported from module ‘OpenGL.GL3’ before it is required

这里需要将Build Settings里的Enable Modules(C and Objective-C)设为No即可。最后别忘了Link OpenGL库本身。

“Mac OpenGL踩坑记”的4个回复

  1. 还是出了问题…
    Undefined symbols for architecture x86_64:
    “_glfwInit”, referenced from:
    _main in main.o
    ld: symbol(s) not found for architecture x86_64

    求助

    1. 你的glfw库文件有问题,重新生成一下,记得要是64位的,可以看看我的《Mac SOIL踩坑记》。

回复 yarpee 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据