vcpkg + mingw 在windows下构建cmake项目

最近接触c++项目开发,尤其是windows平台下,最头疼的是引入其他库时往往需要获取源码编译,通过ide引入静态库、动态库什么的,其步骤非常繁琐,还容易出错。

为了构建跨平台项目,选择了cmake + mingw,换句话来说放弃了宇宙第一IDE Visual Studio。同时选择了vcpkg作为项目的包管理工具。

vcpkg是微软开发的跨平台用于管理c和c++库,可以理解成Java项目下的Maven,js项目npm进行包/库管理工具。

完成本博客之前通过两篇博客激发了灵感:

  1. vcpkg+CLion+cmake+MinGW使用
  2. Visual Studio开源库集成器Vcpkg全教程–利用Vcpkg轻松集成开源第三方库

当然官方文档很重要:vcpkg官方文档

前置条件

  • cmake
  • mingw-posix
  • vscode 用于构建开发环境

vcpkg

vcpkg 有两种用法,一种是作用于全局,即所有项目都共享同一库。另一种则是作为项目的子模块,好处是在CmakeLists.txt设置仅需要设置相对路径即可

set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake
  CACHE STRING "Vcpkg toolchain file")

下面我就以vcpkg作为子模块进行演示。

  1. 创建一个项目demo

  2. demo目录下clone vcpkg项目,并编译

    E:\demo> git clone git@github.com:microsoft/vcpkg.git
    E:\demo\vcpkg> ./bootstrap-vcpkg.bat
    

    接下来可以正常使用了,详情请见vcpkg项目

  3. 安装opengl,glfw3,glad

    E:\demo\vcpkg> vcpkg install opengl:x64-mingw-static
    E:\demo\vcpkg> vcpkg install glfw3:x64-mingw-static
    E:\demo\vcpkg> vcpkg install glad[gl-api-33]:x64-mingw-static
    

    安装前可使用 vcpkg search 命令查找库相关信息。同时也可以使用vcpkg --help 查看相关命令

  4. 编写CMakeLists.txt

    cmake_minimum_required(VERSION 3.0.0)
    set(CMAKE_TOOLCHAIN_FILE ./vcpkg/scripts/buildsystems/vcpkg.cmake
        CACHE STRING "Vcpkg toolchain file")
    
    project(startup VERSION 0.1.0)
      
    set(glad_DIR ./vcpkg/installed/x64-mingw-static/share/glad)
    set(glfw3_DIR ./vcpkg/installed/x64-mingw-static/share/glfw3)
    
    find_package(glad CONFIG REQUIRED)
    find_package(glfw3 CONFIG REQUIRED)
    
    add_executable(demo main.cpp)
    
    target_link_libraries(demo PRIVATE glad::glad)
    target_link_libraries(demo PRIVATE glfw)
    
  5. 项目目录树

    ├───build
    ├───vcpkg
    │   CMakeLists.txt
    │   main.cpp 
    

代码样例

#include 
#include 

#include 

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointersq
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }    

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        processInput(window);

        // render
        // ------
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // glfw: terminate, clearing all previously allocated GLFW resources.
    // ------------------------------------------------------------------
    glfwTerminate();
    return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
}

这里使用了learnopengl-你好,窗口作为项目的样例代码,主要目的是测试glad与glfw3库是否能正常使用

你可能感兴趣的:(工程构建,windows,vscode,java)