mac osx中使用CodeLite的OpenGL,GLFW编译环境配置

版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎评论交流。 https://blog.csdn.net/ryinlovec/article/details/62890212

配完openCV配openGL,历史真是惊人的相似……CodeLite的OpenCV环境配置

mac系统自带OpenGL,本来想用glut,然而编译报错说glut已经被osx10.9的系统弃用了。搜索了一下,推荐较多的是glfw,看了下代码也和glut差不多。

首先,为了预防日后不时之需,先存一下网上找的glut版本的代码:

#include <GLUT/GLUT.h>
void display()
 {
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
    glVertex2f(-0.5, -0.5);
    glVertex2f(-0.5, 0.5);
    glVertex2f(0.5, 0.5);
    glVertex2f(0.5, -0.5);
    glEnd();
    glFlush();  
}  
int main(int argc, char ** argv)  
{  
    glutInit(&argc, argv);  
    glutCreateWindow("Glut Demo");  
    glutDisplayFunc(display);  
    glutMainLoop();  
}  

接下来是glfw的配置。

1.使用homebrew安装glfw(真的超方便!)

brew install glfw

2.GLFW的示例代码

#include <GLFW/glfw3.h> 
int main(int argc, char ** argv)
{
    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();
    }

    glfwTerminate();
    return 0;
}

3.编译与链接

编译选项

gcc main.c -framework OpenGL -I/usr/local/include/ -L/usr/local/lib/ -lglfw

有四项必须的内容:

-framework用来链接mac自带的OpenGL

-I的路径为头文件目录(I即为Include的缩写)

-L的路径为动态链接库目录(L即为Linker的缩写)

-lxxx表示链接libxxx.dylib的文件,比如libglfw.dylib,就写-lglfw

Codelite的设置

在IDE里进行compiler和linker的相应设置。依然坚持不用Xcode,用的是CodeLite。

右键某个project,点击settings,如图。

编译器头文件设置(即-I),在Include Paths一栏添加路径↓

编译器头文件设置

链接库设置(-L,-l和-framework),更改的是第2、3、4栏↓

链接库设置

猜你喜欢

转载自blog.csdn.net/ryinlovec/article/details/62890212