您的位置:首页 > 运维架构

mac 下 不用Xcode, 用gcc自己编译OpenGL工程

2015-11-29 15:17 369 查看
最近想在mac做OpenGL,但我又不想用xcode,xcode开销太大了,一点都不爽快,于是我尝试着用vim开发,gcc编译。经过2天的配置,终于配置出来了。

大概步骤如下:

1)安装代码编辑、编译工具,编辑器我用的是vim,这个系统默认安装好了。编译器我用的是gcc,这个好像也是自带的,还是装xcode的时候装的,不清楚,因为我装这些之前已经装好xcode了,可以敲命令:gcc -version查看。没有的话,可以先装个homebrew工具(参考homebrew安装)真的好用,谁用谁知道,然后brew install gcc49安装gcc49,vim也可以用homebrew安装。

2)安装OpenGL,不过听说mac系统已经安装好了,我不知道是系统自带还是xcode带进来的,我查了一下,确实有,include默认路径里包括glut库,在Library中还可以找的到glut的framework。可以直接调用。但是,glut库已经不更新了,直接用也可以通过,但是编译器会弹出一大堆的警告,说glut已经废弃之类的话。为了fashion一点,我决定再装freeglut或glfw,网上查了一下,说freeglut还不稳定,我也看了一下api,感觉freeglut还是封装的过了点,渲染的while循环都封装起来了,而glfw还保留了opengl原始的渲染逻辑,看的清楚一点,所以我选择了glfw,同样的道理,命令行:brew
install glfw3,搞定,只能说homebrew确实好用。。。

3)命令行vim firstOpengl.c,开始编辑代码了,网上随便复制一段:

#include <GLFW/glfw3.h>

int main(void)
{
GLFWwindow* window;

/* Initialize the library */
if (!glfwInit())
return -1;

/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(480, 320, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}

/* Make the window's context current */
glfwMakeContextCurrent(window);

/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Draw a triangle */
glBegin(GL_TRIANGLES);

glColor3f(1.0, 0.0, 0.0); // Red
glVertex3f(0.0, 1.0, 0.0);

glColor3f(0.0, 1.0, 0.0); // Green
glVertex3f(-1.0, -1.0, 0.0);

glColor3f(0.0, 0.0, 1.0); // Blue
glVertex3f(1.0, -1.0, 0.0);

glEnd();

/* Swap front and back buffers */
glfwSwapBuffers(window);

/* Poll for and process events */
glfwPollEvents();
}

:qw保存,敲命令:gcc firstOpengl.c -o run -lglfw3 -framework OpenGL
再敲命令:./run运行



好了,一个运行在mac上,用vim编辑,gcc编译的opengl工程跑起来了。

多说一句:再按 command+Q,可退出窗口回到终端。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: