背景:GLFW
一、下载和编译
-
从官网下载源代码包:http://www.glfw.org/download.html
(我下载的是 github 仓库上的) -
按官方指南编译。总结如下:
cd glfw-master
cmake .
# 默认是编译静态库,如果要编译动态库则 cmake -DBUILD_SHARED_LIBS=ON .
make
make install
最后会看到
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
二、配置 XCode 项目
注:配置名字可以通过cmd+f搜索来定位
-
配置项目的Build Settings:
-
Other Linker Flags 为
-lgfw3
-
Library Search Paths 的 Debug 和 Release 分别加上
/usr/local/lib/
-
Header Search Paths 加上
/usr/local/include/
-
-
添加库,打开项目的Build Phases
在 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;
}
点击运行按钮。效果如下: