ubuntu18.04+CLion配置GLFW

折腾半天终于解决这个在ubuntu18和CLion下配置GLFW了,真心麻烦!!!
看了不知道多少博客教程,还有官方的哪个文档,都没成功,现在终于弄好了,其实glfw在ubuntu中编译安装什么都很方便的!
如果我的方法有效的话就点个赞让别人看到,没有用请留言!
注意
我在这个之前就装了OpenGL
环境
ubuntu18.04 CLion2018 Cmake 3.12.0

第一步
下载glfw,地址
解压:得到文件夹:glfw-3.2.1
第二步
编译glfw:
1)进入 glfw3-3.x.x 目录,建立build子目录, 命令行执行 cmake-gui, 源码目录选择glfw3-3.x.x, 目标目录选择build。 configure,generate
2)命令行模式,cd build,执行 make, sudo make install .
Cmakelist.txt
刚开始用Clion,对cmake也不了解,尝试了很多配置就这个有效:

project(open_window)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(open_window glfw3 X11 Xrandr Xi Xinerama Xxf86vm Xcursor GL GLEW pthread dl)

测试代码

#include 
#include 
#include 
#include 
#include 
#include 
using namespace glm;
using namespace std;
int main()
{
    // Initialise GLFW
    glewExperimental = true; // Needed for core profile
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }
    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL

// Open a window and create its OpenGL context
    GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
    window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
    if( window == NULL ){
        fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window); // Initialize GLEW
    glewExperimental=true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    do{
        // Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
        glClear( GL_COLOR_BUFFER_BIT );

        // Draw nothing, see you in tutorial 2 !

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();

    } // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
           glfwWindowShouldClose(window) == 0 );
}

你可能感兴趣的:(OpenGL)