Skip to main content

Create first Conan project

Install Conan​

pip install conan==1.59.0

main.cpp​

#include <fmt/printf.h>
#include <nlohmann/json.hpp>

int main()
{
nlohmann::json json = {
{"pi", 3.14},
{"happy", true},
{"name", "Kuba"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 2, 3}},
{"object", {
{"currency", "PLN"},
{"value", 100.0}
}}
};

fmt::print("JSON: {}\n", json);
return 0;
}

conanfile.txt​

[requires]
nlohmann_json/3.9.1
fmt/7.1.3

[generators]
cmake

CMakeLists.txt​

cmake_minimum_required(VERSION 3.15)

project(conan_example LANGUAGES CXX)

add_compile_options(-std=c++17)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(conan_example
main.cpp
)

target_link_libraries(conan_example
PRIVATE ${CONAN_LIBS}
)
note

Assuming that we correctly installed dependencies with Conan, included the conanbuildinfo.cmake file and called conan_basic_setup() (defined in conanbuildinfo.cmake) there should be some confirmation logs about Conan in the CMake output.

Note, that the CMake generator creates a variable CONAN_LIBS which groups all the CMake targets that represent installed dependencies. The only thing that we need to do is to use this helper variable in the target_link_libraries() command (CMakeLists.txt).

Build​

mkdir build
cd build
conan install .. --build missing
cmake .. -GNinja
cmake --build .