ENG | Getting started with Conan
This article describes the very first steps with Conan2 on Windows
I adopting CMakefiles for Windows (they work on Linux with libraries installed system-wide by distribution’s package manager) a bit frustrating so I decided to use Conan package repository which we use at work.
I suggest closing Visual Studio during conan install
command.
Installation
Installation is described in official documentation - installation.
It’s really installing a single Python package, but let’s upgrade Python and Pip package manager also.
1
2
3
winget update Python
py -m pip install --upgrade pip
pip install conan
Let’s proceed with Build a simple CMake project using Conan and adopt our last example. Use conan profile detect --force
if profile already exists.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PS C:\dev-c\slimemold> conan profile detect
detect_api: Found msvc 17
Detected profile:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.version=194
os=Windows
WARN: This profile is a guess of your environment, please check it.
WARN: The output of this command is not guaranteed to be stable and can change in future Conan versions.
WARN: Use your own profile files for stability.
Saving detected profile to C:\Users\pavel\.conan2\profiles\default
Try to list remote servers.
1
2
PS C:\dev-c\slimemold> conan remote list
conancenter: https://center2.conan.io [Verify SSL: True, Enabled: True]
Creating CMakeLists.txt and conanfile.txt for SDL2
Now search for SDL2 library
Sadly it’s not the latest version (at time of writing latest is 2.32.8), we can compile it ourselves and host in own repository, but for now I’m fine with old version.
Let’s create conanfile.txt
and simplify our CMakeLists.txt
1
2
3
4
5
6
7
8
9
[requires]
sdl/2.32.2
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cmake_minimum_required(VERSION 3.20)
project(slime_mold_simulator)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Set default build type to Release
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
find_package(SDL2 REQUIRED)
add_executable(slime_mold src/slimemold.cpp)
target_link_libraries(slime_mold PRIVATE SDL2::SDL2)
{ file=”CMakeLists.txt” }
Now let’s try installing project dependencies specified in the conanfile.txt
.
Note: If you specify
cmake_layout
inconanfile.txt
, thebuild
folder is created automatically. When it’s not specified useconan install . --output-folder=build
otherwise many files will end up in project’s root directory. When you use both layout and output-folder, they will end up in build/build directory. 🤯
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
PS C:\dev-c\slimemold> conan install .
======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=23
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=194
os=Windows
Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=23
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=194
os=Windows
======== Computing dependency graph ========
sdl/2.32.2: Not found in local cache, looking in remotes...
sdl/2.32.2: Checking remote: conancenter
Connecting to remote 'conancenter' anonymously
sdl/2.32.2: Downloaded recipe revision c7ab0fd21ad5f0bd48e9c3cc3f14995b
cmake/3.31.8: Not found in local cache, looking in remotes...
cmake/3.31.8: Checking remote: conancenter
cmake/3.31.8: Downloaded recipe revision dde3bde00bb843687e55aea5afa0e220
Graph root
conanfile.txt: C:\dev-c\slimemold\conanfile.txt
Requirements
sdl/2.32.2#c7ab0fd21ad5f0bd48e9c3cc3f14995b - Downloaded (conancenter)
Build requirements
cmake/3.31.8#dde3bde00bb843687e55aea5afa0e220 - Downloaded (conancenter)
Resolved version ranges
cmake/[>3.27 <4]: cmake/3.31.8
======== Computing necessary packages ========
sdl/2.32.2: Main binary package '3ddd432baf547d46b98663d0393bf96391bbf9d1' missing
sdl/2.32.2: Checking 1 compatible configurations
sdl/2.32.2: Compatible configurations not found in cache, checking servers
sdl/2.32.2: '3789fd125ff78fd28e4f3506bbad4cde7783a7dd': compiler.version=193
sdl/2.32.2: Found compatible package '3789fd125ff78fd28e4f3506bbad4cde7783a7dd': compiler.version=193
Requirements
sdl/2.32.2#c7ab0fd21ad5f0bd48e9c3cc3f14995b:3789fd125ff78fd28e4f3506bbad4cde7783a7dd#b0e3f6597fbfc00eb7f385f59aed433f - Download (conancenter)
Build requirements
Skipped binaries
cmake/3.31.8
======== Installing packages ========
-------- Downloading 1 package --------
sdl/2.32.2: Retrieving package 3789fd125ff78fd28e4f3506bbad4cde7783a7dd from remote 'conancenter'
sdl/2.32.2: Package installed 3789fd125ff78fd28e4f3506bbad4cde7783a7dd
sdl/2.32.2: Downloaded package revision b0e3f6597fbfc00eb7f385f59aed433f
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated: 'cpp_info.names' used in: sdl/2.32.2
======== Finalizing install (deploy, generators) ========
conanfile.txt: Writing generators to C:\dev-c\slimemold\build\generators
conanfile.txt: Generator 'CMakeDeps' calling 'generate()'
conanfile.txt: CMakeDeps necessary find_package() and targets for your CMakeLists.txt
find_package(SDL2)
target_link_libraries(... SDL2::SDL2main)
conanfile.txt: Generator 'CMakeToolchain' calling 'generate()'
conanfile.txt: CMakeToolchain generated: conan_toolchain.cmake
conanfile.txt: CMakeToolchain: Preset 'conan-default' added to CMakePresets.json.
(cmake>=3.23) cmake --preset conan-default
(cmake<3.23) cmake <path> -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=generators\conan_toolchain.cmake -DCMAKE_POLICY_DEFAULT_CMP0091=NEW
conanfile.txt: CMakeToolchain generated: C:\dev-c\slimemold\build\generators\CMakePresets.json
conanfile.txt: CMakeToolchain generated: C:\dev-c\slimemold\CMakeUserPresets.json
conanfile.txt: Generating aggregated env files
conanfile.txt: Generated aggregated env files: ['conanbuild.bat', 'conanrun.bat']
Install finished successfully
Now open Visual Studio and you should be able to build the project. Curiously, target is conan-release
Use
conan install . --build=missing -s build_type=Debug
orconan install . --build=missing -s build_type=RelWithDebInfo
Further steps
- Conan uses
compiler.cppstd=14
.- We can modify this in
C:\Users\[username]\.conan2\profiles\default
tocompiler.cppstd=14
. - We can override it for project
conan install . --output-folder=build -s compiler.cppstd=23
- We can put this into
conanfile.txt
1 2
[settings] compiler.cppstd=23
- We can modify this in
- At work, we have
build_type=RelWithDebInfo
and Conan installed inside Python virtual environment. - If you have company repository at
C:\Users\[username]\.conan2\remotes.json
you may need to runconan remote login gitlab-conan2 access-token-read -p Y0ur$ecr3tPassw0rd
or something similar.
Useful commands
1
2
3
4
# List package in cache
conan list
# List with detailed info (debug, release build, build options, ...)
conan list sdl:*
Final words
Here I found that Conan has some pros and cons and it’s not all black and white.
I used mostly FetchContent in CMakeLists.txt
files to download certain version of library from GitHub and compile them with every project. It’s simple, it works, it consumes some bandwidth and it’s not suitable for some large libraries as OpenCV where I usually used hardcoded paths on Windows, paths in system enviroment and so on. Which is not convenient at all.
Conan solves these problems, but there are another ones. I have to rely on third party repository and it’s possible that after few years, version specified in my conanfile.txt
disappears. Or I can create my own package repository on Linux server behind firewall and whoever wants to use my own project might be out of luck. Or I’m forcing someone to use exactly the same version of library as me. Which may and may not be a good thing. It depends.
For corporate or personal usage, Conan is IMHO perfectly fine as it allows reproducible builds. For corporate use case it’s a bit inconvenient to track which dependencies are needed as .conan
directory can get quite big with multiple version of Qt library build for both debug and release and so on. But this is an issue even without Conan.
Files
- slimemold_conan_sdl2.7z Slimemold example from the last article adopted to Conan …
- slimemold_conan_sdl3.7z … and ported to SDL3 …
- GitHub repository … and with added controls using Dear ImGui