现代cmake从github引⼊三⽅库,使⽤FetchContent(3.14以上版
本)
使⽤FetchContent的步骤总结起来就是:
在cmake⽂件写⼊  include(FetchContent) ,具体看完整实例
使⽤FetchContent_Declare(三⽅库) 获取项⽬。可以是⼀个URL也可以是⼀个Git仓库。
使⽤FetchContent_MakeAvailable(三⽅库) 获取我们需要库,然后引⼊项⽬。
使⽤ target_link_libraries(项⽬名PRIVATE 三⽅库::三⽅库)
第⼀步:
  在 ⽂件写⼊ include(FetchContent)
第⼆步:
  # FetchContent_Declare
第三部
  # FetchContent_MakeAvailable
  FetchContent_MakeAvailable(json)
第四步
  # target_link_libraries
  target_link_libraries(项⽬名字PRIVATE nlohmann_json::nlohmann_json)
看⼀下完整的cmake
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 gitee/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 gitee/mai12/spdlog.git
GIT_TAG v1.4.1)
FetchContent_MakeAvailable(spdlog)
target_link_libraries(mjson PRIVATE spdlog::spdlog)
下⾯是如何使⽤
//
// Created by ct on 2020/9/3.
//
#include <iostream>
#include <spdlog/spdlog.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;cmake如何使用
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 = place("three", 3);
// the following call will not add an object, because there is already
// a value stored at key "B"
auto res2 = place("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++");
return0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。