配置:OS X 10.11.2 + Cmake3.5 + GLFW3.1 + GLEW2.0 + OpenGL4.1 + Qt Creator3.5
本文假设已安装cmake,QT和QT Creator,若为安装请自行google。
glfw和glew可通过两种方式安装,一是用homebrew包管理安装,但我在安装时因为源问题,访问速度慢,终端挂vpn也不行,所以放弃并使用第二种方案;二是通过下载源码,用cmake/make编译安装,推荐使用第二种。
GLFW 和 GLEW安装
GLFW
下载GLFW,使用cmake/make
**Generating files with the CMake command-line tool **
To make an in-tree build, enter the root directory of the GLFW source tree (i.e. not the src subdirectory) and run CMake. The current directory is used as target path, while the path provided as an argument is used to find the source tree.
cdcmake .
To make an out-of-tree build, make a directory outside of the source tree, enter it and run CMake with the (relative or absolute) path to the root of the source tree as an argument.
mkdir glfw-build cd glfw-build cmakemake make install
**CMake options ** 编译生成动态链接库,需更改cmake配置。 命令行cmake -DBUILD_SHARED_LIBS=ON .
,或用GUI设置BUILD_SHARE_LIBS为ON
###GLEW 源码地址,下载glew-2.0.0.zip。(source code.zip缺失部分代码,编译不通过)。
**Build **
$ make$ sudo make install$ make clean
glfw/glew编译安装一般生成include目录,还有动态链接库,osx下目录为:/usr/local/include 、/usr/local/lib.
Qt Creator运行OpenGL
新建工程,配置文件类似如下:
QT += coreQT -= guiTARGET = QtOpenGL-osxCONFIG += consoleCONFIG -= app_bundleTEMPLATE = appSOURCES += main.cpp#include glew/glfwINCLUDEPATH += /usr/local/include#lib glew / glfw.LIBS += -L/usr/local/lib -lglfw -lglew#osx opengl frameworkQMAKE_LFLAGS += -F/System/Library/Frameworks/LIBS += -framework OpenGL \ -framework Cocoa \ -framework CoreVideo \ -framework IOKit \
Xcode运行openGL
1、打开Xcode,新建OSX Command Line Tool工程
2、左侧选中工程
(1)在 Build Settings 里找到 Header Search Paths
添加终端中输出的头文件路径:/usr/local/include/
(2)在 Build Settings 里找到 Library Search Paths
添加终端中输出的lib文件路径:/usr/local/lib/
3、在 Build Phases中的 Link Binary With Libraries 中,添加:
(1)IOKit.framework
(2)Cocoa.framework
(3)OpenGL.framework
(4)CoreVideo.framework
(5)libglfw3.a
若不能找到 libglfw3.a ,可通过 Add Other... 按钮手动定位该文件,文件在 /usr/local/lib/ 中。