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)

指定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之后,将在本地作用域中定义以下变量,其中<dependency>是依赖项的名称。
<dependency>_SOURCE_DIR:依赖性源码路径
<dependency>_BINARY_DIR :依赖项编译路径
<dependency>_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 <iostream>
#include <nlohmann/json.hpp>
#include <json/json.h>

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
运行

./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

相关推荐
R_.L2 小时前
C++:哈希(哈希冲突的解决)
c++·哈希算法
tangchao340勤奋的老年?3 小时前
QTcpSocket(客户端实现)多线程连接慢问题
c++·qt
ElseWhereR5 小时前
机器人能否回到原点 - 简单
c++·算法·leetcode
泛舟起晶浪5 小时前
海底高铁--差分
数据结构·c++·算法
QuantumStack7 小时前
【C++ 真题】P1109 学生分组
java·数据结构·c++·算法
basketball6167 小时前
C++ set容器总结
开发语言·c++
pystraf7 小时前
洛谷 P10463 Interval GCD Solution
数据结构·c++·算法·线段树
我的sun&shine8 小时前
高级数据结构01BST树
数据结构·c++·算法
techdashen8 小时前
性能比拼: Rust vs C++
java·c++·rust
二进制人工智能10 小时前
【QT5 多线程示例】线程池
开发语言·c++·qt