背景:GLFWhtml
從官網下載源代碼包:http://www.glfw.org/download.html
(我下載的是 github 倉庫上的)git
按官方指南編譯。總結以下:github
cd glfw-master cmake . # 默認是編譯靜態庫,若是要編譯動態庫則 cmake -DBUILD_SHARED_LIBS=ON . make make install
最後會看到shell
Install the project... -- Install configuration: "" -- Installing: /usr/local/include/GLFW -- Installing: /usr/local/include/GLFW/glfw3.h -- Installing: /usr/local/include/GLFW/glfw3native.h -- Installing: /usr/local/lib/cmake/glfw3/glfw3Config.cmake -- Installing: /usr/local/lib/cmake/glfw3/glfw3ConfigVersion.cmake -- Installing: /usr/local/lib/cmake/glfw3/glfw3Targets.cmake -- Installing: /usr/local/lib/cmake/glfw3/glfw3Targets-noconfig.cmake -- Installing: /usr/local/lib/pkgconfig/glfw3.pc -- Installing: /usr/local/lib/libglfw3.a
注:配置名字能夠經過cmd+f搜索來定位xcode
配置項目的Build Settings:ide
Other Linker Flags 爲 -lgfw3
ui
Library Search Paths 的 Debug 和 Release 分別加上/usr/local/lib/
code
Header Search Paths 加上 /usr/local/include/
htm
添加庫,打開項目的Build Phasesblog
在 Link Binary With Libraries 中添加:
Cocoa
OpenGL
IOKit
CoreVideo
新建main.cpp:
#include <GLFW/glfw3.h> int main(int argc, const char * argv[]) { GLFWwindow* win; if(!glfwInit()){ return -1; } win = glfwCreateWindow(640, 480, "OpenGL Base Project", NULL, NULL); if(!win) { glfwTerminate(); return -1; } glfwMakeContextCurrent(win); while(!glfwWindowShouldClose(win)){ glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES); { glColor3f(1.0,0.0,0.0); glVertex2f(0, .5); glColor3f(0.0,1.0,0.0); glVertex2f(-.5,-.5); glColor3f(0.0, 0.0, 1.0); glVertex2f(.5, -.5); } glEnd(); glfwSwapBuffers(win); glfwPollEvents(); } glfwTerminate(); return 0; }
點擊運行按鈕。效果以下: