C++包管理利器CPM

C++包管理利器CPM

一、介绍

CPM.cmake is a cross-platform CMake script that adds dependency management capabilities to CMake. It’s built as a thin wrapper around CMake’s FetchContent module that adds version control, caching, a simple API and more.
CPM.cmake是一个与CMake配合使用的C++包管理工具,更准确说是依赖管理,它主要用于简化C++项目中对第三方依赖引入的复杂性。 通过使用CPM,开发者可以更轻松地将所需的第三方库集成到他们的项目中,而无需手动下载、配置和管理这些库。CPM提供了一个简洁的语法,使开发者能够以声明式的方式指定项目所需的依赖项,并自动处理其下载、构建和安装过程。这样,开发者可以更专注于项目本身的开发,而不必花费过多时间和精力来处理依赖项的繁琐细节。总之,CPM的出现大大简化了C++项目的依赖管理工作,提高了开发效率。
总结:包管理、依赖管理、跨平台、轻量化、即插即用、语法简单

二、CPM语法

CPMAddPackage(
  NAME          # The unique name of the dependency (should be the exported target's name)
  VERSION       # The minimum version of the dependency (optional, defaults to 0)
  OPTIONS       # Configuration options passed to the dependency (optional)
  DOWNLOAD_ONLY # If set, the project is downloaded, but not configured (optional)
  [...]         # Origin parameters forwarded to FetchContent_Declare, see below
)
  • […] 指定第三方依赖的源位置(支持Github, Gitlab, 和指定URL)
  • 提供GIT_REPOSITORY、GIT_REPOSITORY、GIT_TAG 或者直接提供URL

指定URL示例

CPMAddPackage("https://example.com/my-package-1.2.3.zip")

CPMAddPackage("https://example.com/my-package-1.2.3.zip#MD5=68e20f674a48be38d60e129f600faf7d")

CPMAddPackage("https://example.com/[email protected]")

指定版本用@version, 比如[email protected]
指定tag用#tag,比如xxx#1.2.1
针对github,可以简写成gh:user/[email protected]
针对gitlab, 可以简写成gl:user/[email protected]

jsoncpp 引入示例

CPMAddPackage(
    NAME jsoncpp
    GITHUB_REPOSITORY open-source-parsers/jsoncpp
    GIT_TAG 1.9.5
    OPTIONS "JSONCPP_WITH_TESTS OFF"
)

# 简化
CPMAddPackage("gh:open-source-parsers/jsoncpp#1.9.5")

特别说明

在调用CPMAddPackage之后,将在本地作用域中定义以下变量,其中是依赖项的名称。
_SOURCE_DIR:依赖性源码路径
_BINARY_DIR :依赖项编译路径
_ADDED : 依赖项是否被添加:YES(之前没有被添加过),NO(反之)
比如:

jsoncpp_SOURCE_DIR
jsoncpp_BINARY_DIR
jsoncpp_ADDED

三、工程引入

1、CPM.cmake引入

手动下载引入

直接下载CPM.cmake脚本(假定下载到/your_project_path/cmake/CPM.cmake),然后在CMakeLists.txt中include即可。

# CMakeLists.txt

include(your_project_path/cmake/CPM.cmake)
自动下载引入

直接在CMakeLists.txt中使用 file 指令自动下载,然后include。

# CMakeLists.txt

# download CPM.cmake
file(
  DOWNLOAD
  https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.38.7/CPM.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake
)
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)

2、 测试代码

工程目录
.
├── CMakeLists.txt
└── main.cpp
CMakeLists.txt
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(jsontest)

# download CPM.cmake
file(
  DOWNLOAD
  https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.38.7/CPM.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake
)
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)

# ---- Dependencies ----
CPMAddPackage("gh:nlohmann/[email protected]")
CPMAddPackage(
    NAME jsoncpp
    GITHUB_REPOSITORY open-source-parsers/jsoncpp
    GIT_TAG 1.9.5
    OPTIONS "JSONCPP_WITH_TESTS OFF"
)
# CPMAddPackage("gh:open-source-parsers/jsoncpp#1.9.5")
# ---- Executable ----

add_executable(jsontest main.cpp)
target_link_libraries(jsontest nlohmann_json jsoncpp_lib)
main.cpp
#include 
#include 
#include 

int main() {
    auto j = nlohmann::json::parse(R"({"happy": true, "pi": 3.141})");
    std::cout << "test nlohman_json: " << j.dump(2) << std::endl;

    Json::Value root;
    root["action"] = "run";
    root["number"] = 1;
    Json::FastWriter writer;
    std::cout << "test jsoncpp: " << writer.write(root) << std::endl;

    return 0;
}
cmake构建
cmake -B build
cmake --build build

C++包管理利器CPM_第1张图片

运行

./build/bin/jsontest

test nlohman_json: {
  "happy": true,
  "pi": 3.141
}
test jsoncpp: {"action":"run","number":1}

四、参考

1. CPM: https://github.com/cpm-cmake/CPM.cmake
2. CPM: An Awesome Dependency Manager for C++ with CMake: https://medium.com/swlh/cpm-an-awesome-dependency-manager-for-c-with-cmake-3c53f4376766
3. FetchContent: https://cmake.org/cmake/help/latest/module/FetchContent.html

你可能感兴趣的:(c++,cmake,c++)