This repository shows how to create a basic Conan example package and how to build code consuming it using CMake.
Install Conan in your system:
pip3 install conan
We will use Conan to create a package with testing. Conan assumes by default that the package will be an external project checking out the source from git. Here, we want to have the package information along the source code. For simplicity, we will re-use the source code provided by Conan as example.
Create the package
folder:
mkdir package && cd package
Create the package recipe (conanfile.py
), the package testing folder(test_package
) and the source folder (src
):
conan new Hello/0.1 -t -s
Build the package for the demo
user and testing
channel, testing using test_package
and add it to the local repository:
conan create . demo/testing
Inspect the package in the local repository:
conan info Hello/0.1@demo/testing
To consume a package we must create a file to declare the dependency and invoke the Conan CLI to satisfy it right before building with CMake.
Create the consumer
folder:
mkdir consumer && cd consumer
Create main.cpp
containing:
#include <iostream>
#include "hello.h"
int main() {
hello();
}
Create conanfile.txt
declaring a dependency on the package and that a CMake support file must be generated:
[requires]
Hello/0.1@demo/testing
[generators]
cmake
Create CMakeLists.txt
including the conanbuildinfo.cmake
that will be generated by Conan and invoking the setup conan_basic_setup
function declared in that file:
cmake_minimum_required(VERSION 3.10)
project(hello-consumer CXX)
# Setup Conan packages ('conan install' must be run first)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_executable(main main.cpp)
target_link_libraries(main ${CONAN_LIBS})
Create the build
folder
mkdir build && cd build
Install packages and generate the required conanbuildinfo.cmake
CMake support file. The path points to the location of the conanfile.txt
file:
conan install ..
Configure and generate CMake build:
cmake -DCMAKE_BUILD_TYPE=Release ..
Build:
cmake --build .
Run:
bin/main
This is a variation of the previous, automatically installing the packages from CMake to avoid having to use the CLI.
Create the consumer-cmake
folder:
mkdir consumer-cmake && cd consumer-cmake
Create main.cpp
containing:
#include <iostream>
#include "hello.h"
int main() {
hello();
}
Create CMakeLists.txt
which will download a conan.cmake
CMake wrapper file which provides the conan_cmake_run
function to install package dependencies from whithin CMake instead of having to invoke the CLI:
cmake_minimum_required(VERSION 3.10)
project(hello-consumer-cmake CXX)
# Download automatically, you can also just copy the conan.cmake file
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/ conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/ master/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake")
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
# Install required packages
conan_cmake_run(REQUIRES Hello/0.1@demo/testing
BASIC_SETUP)
add_executable(main main.cpp)
target_link_libraries(main ${CONAN_LIBS})
Configure and generate CMake build:
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
Build:
cmake --build .
Run:
bin/main