Skip to content

Instantly share code, notes, and snippets.

@ForgottenUmbrella
Last active April 15, 2024 07:08
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ForgottenUmbrella/0f32f6446b2948a3a5a99687b264910d to your computer and use it in GitHub Desktop.
Save ForgottenUmbrella/0f32f6446b2948a3a5a99687b264910d to your computer and use it in GitHub Desktop.
How to use Conan, a C++ package manager, for beginners

Package Management in C++ with Conan for Beginners

C++ package management can be complicated.

Below are some key tools involved:

Make

Make runs commands defined in a Makefile, for example, to build and install programs with the compiler and linker. For our purposes, we won't worry about what this looks like; you only need to understand its purpose in relation to CMake.

CMake

CMake is a build system, among other things. It can be tedious to write Makefiles, so developers use CMake to automatically create one from a CMakeLists.txt.

Of course, developers may use another build system (which might not require Make), but for this tutorial, we'll focus on CMake.

Conan

Conan installs dependencies listed in a conanfile.txt, and also prepares the build system to link with the dependencies.

Using Conan

Note that I will be using $variable syntax to show what you'll need to replace. Also note that ${VARIABLE} is not something you need to replace.

  1. Write a conanfile.txt stating your dependencies and build system:
[requires]
# You may have multiple lines like the one below, if you have many dependencies.
$library/$version@$owner/$branch

[generators]
cmake

Here, we're using the cmake generator to generate a conanbuildinfo.cmake file, which will later be used in your CMakeLists.txt to create a Makefile. If you're using another build system, see the docs on generators.

  1. Write a file for your specific build system (in our case, a CMakeLists.txt file), that will generate a Makefile for building your program:
cmake_minimum_required(VERSION $version)  # Specifies the required CMake version.
project($project_name)  # Defines the project name.

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)  # Includes the contents of the conanbuildinfo.cmake file.
conan_basic_setup()  # Prepares the CMakeList.txt for Conan.

# $source_files is a space-delimited list of filenames.
add_executable($executable_name $source_files)  # Specifies the executable to build.
target_link_libraries($executable_name ${CONAN_LIBS})  # Specifies what libraries to link, using Conan.

Above is the bare minimum for a CMakeLists.txt file. If you would like to learn more about using CMake, see the CMake tutorial.

  1. Create a build directory with mkdir build, and cd build.

  2. Run conan install, passing the directory where your conanfile.txt is, to download and install dependencies and generate the conanbuildinfo.cmake used by CMakeLists.txt. For example, run conan install .. if your conanfile.txt is in the parent directory.

  3. Run the build system, cmake, passing the directory containing your CMakeLists.txt, to create the Makefile.

  4. Run cmake --build . or make to build your program using the generated Makefile in the build directory.

That covers the basic use case, but for more info, see the Conan docs.

@rathod-sahaab
Copy link

Thanks really helped me out!

@wdavidcalsin
Copy link

Simple concise and straightforward.

@rathod-sahaab
Copy link

https://github.com/rathod-sahaab/cpp-conan-template

If someone wants a quick setup repo

@alfiankan
Copy link

yass

@florianwns
Copy link

Thx you so much !

Also, There is a good tutorial for clion here : https://docs.conan.io/1/integrations/ide/clion.html

@lkcole
Copy link

lkcole commented Dec 19, 2023

appears conanbuildinfo.cmake is deprecated for conan 2.0... any chance of an updated (@rathod-sahaab, @ForgottenUmbrella )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment