Управление зависимостями C++. CMake
Работает только с проектами которые так же собираются CMake
Минимальная структура проекта
.
├── CMakeLists.txt
└── src
└── hello.cpp
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(Tutorial)
add_executable(${PROJECT_NAME} src/hello.cpp)
include(FetchContent)
FetchContent_Declare(
json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.11.2
)
FetchContent_Declare(
zlib
GIT_REPOSITORY https://github.com/madler/zlib.git
GIT_TAG v1.3
)
FetchContent_MakeAvailable(json)
FetchContent_MakeAvailable(zlib)
target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json)
target_link_libraries(${PROJECT_NAME} PRIVATE zlib)
hello.cpp
#include <iostream>
#include <zlib.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
std::cout << zlibVersion();
std::cout << "\n";
json ex1 = json::parse(R"(
{
"pi": 3.141,
"happy": true
}
)");
std::cout << ex1.contains("pi2");
std::cout << "\n";
std::cout << ex1;
return 0;
}
Сборка
mkdir build && cd build
cmake ..
make