使用FetchContent的步骤:

 1. 在cmake文件写入  include(FetchContent) ,具体看完整实例.
 2. 使用FetchContent_Declare(三方库) 获取项目。可以是一个URL也可以是一个Git仓库。
 3. 使用FetchContent_MakeAvailable(三方库) 获取我们需要库,然后引入项目。
 4. 使用 target_link_libraries(项目名PRIVATE 三方库::三方库)

1、一个测试例子

源码:

//
// Created by ct on 2020/9/3.
//

#include <iostream>
#include <spdlog/spdlog.h>
#include <nlohmann/json.hpp>

using json = nlohmann::json;
int runner(){
    // create JSON values
    json object = {{"one", 1}, {"two", 2}};
    json null;

    // print values
    std::cout << object << '\n';
    std::cout << null << '\n';

    // add values
    auto res1 = object.emplace("three", 3);
    null.emplace("A", "a");
    null.emplace("B", "b");

    // the following call will not add an object, because there is already
    // a value stored at key "B"
    auto res2 = null.emplace("B", "c");

    // print values
    std::cout << object << '\n';
    std::cout << *res1.first << " " << std::boolalpha << res1.second << '\n';

    std::cout << null << '\n';
    std::cout << *res2.first << " " << std::boolalpha << res2.second << '\n';

    spdlog::info("i love c++");
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.17)
project(mjson)

set(CMAKE_CXX_STANDARD 14)

file(GLOB SOURCES_AND_HEADERS "*.cpp" "include/*.h")

add_executable(mjson main.cpp ${SOURCES_AND_HEADERS})
target_include_directories(${PROJECT_NAME}
        PUBLIC ${PROJECT_SOURCE_DIR}/include
        )
include(FetchContent)

FetchContent_Declare(json
        GIT_REPOSITORY https://gitee.com/slamist/json.git
        GIT_TAG v3.7.3)
FetchContent_MakeAvailable(json)

target_link_libraries(mjson PRIVATE nlohmann_json::nlohmann_json)

FetchContent_Declare(spdlog
        GIT_REPOSITORY https://gitee.com/mai12/spdlog.git
        GIT_TAG v1.4.1)
FetchContent_MakeAvailable(spdlog)
target_link_libraries(mjson PRIVATE spdlog::spdlog)

2、示例二

FetchContent使用步骤

1.include(FetchContent) 
2.FetchContent_Declare(子模块名) 获取项目。
3.FetchContent_MakeAvailable(子模块),再引入我们的项目中
4.target_link_libraries(主项目 PRIVATE 子模块::子模块)

目录结构

.
├───build		# cmake的输出文件
├───ext		# spdlog等第三方库的存放目录
├───include		# 头文件路径
├───src		# 源文件路径
└───CMakeLists.txt # top directory下的cmake配置文件

top directory下的cmake配置文件CMakeLists.txt文件源码

cmake_minimum_required(VERSION 3.18)

project(spdlog_demo VERSION 1.0.1)

# 因为spdlog是基于c++11的库
set(CMAKE_CXX_STANDARD 11)

# 指定源文件
set(src_file 
	${CMAKE_CURRENT_SOURCE_DIR}/src/main.cc
)

# 创建可执行程序项目
add_executable(spdlog_demo ${src_file} )
# 指定头文件路径
target_include_directories(spdlog_demo PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/)
# 指定lib文件路径
target_link_libraries(	spdlog_demo PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

# fetchcontent重点来了
#-------------------------------------------------------------------
include(fetchcontent)      # 照写,不需要修改
 fetchcontent_declare(	spdlog	#库名字
						GIT_REPOSITORY https://gitee.com/mohistH/spdlog.git	# 仓库地址
						GIT_TAG v1.x # 库版本
						SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ext/spdlog # 指定库下载地址
						)

fetchcontent_makeavailable(spdlog)

# 项目中使用spdlog
target_link_libraries(spdlog_demo PRIVATE spdlog::spdlog)
#-------------------------------------------------------------------

这里创建了一个项目spdlog_demo,该项目引用了子模块spdlog,本地没有spdlog,上面的代码中则是在调用cmakelists.txt的时候下载spdlog的源码

转到build目录,使用cmake … , 就开始配置项目了,并下载源码spdlog, 将spdlog的源码放到了top directory下的ext文件夹下。

参考:现代cmake 从github引入三方库,使用FetchContent ( 3.14 以上版本)
参考:FetchContent
参考:cmake之引入外部项目(引用其他项目)、FetchContent管理子模块(fetchcontent用法)

3、自动下载解压依赖库ExternalProject

ExternalProject

SET(ONNXRUNTIME_URL https://github.com/microsoft/onnxruntime/releases/download/v1.8.1/onnxruntime-win-x64-1.8.1.zip)

参考:

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐