天天看點

mac osx中使用CodeLite的OpenGL,GLFW編譯環境配置

配完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(-, -);
    glVertex2f(-, );
    glVertex2f(, );
    glVertex2f(, -);
    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 -;

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

    /* 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(, , );    // Red
        glVertex3f(, , );

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

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

        glEnd();

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

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

    glfwTerminate();
    return ;
}
           

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一欄添加路徑↓

mac osx中使用CodeLite的OpenGL,GLFW編譯環境配置

連結庫設定(-L,-l和-framework),更改的是第2、3、4欄↓

mac osx中使用CodeLite的OpenGL,GLFW編譯環境配置

繼續閱讀