C++ package management can be complicated.
Below are some key tools involved:
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 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 installs dependencies listed in a conanfile.txt, and also prepares the build system to link with the dependencies.
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.
- 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.
- 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.
-
Create a build directory with
mkdir build
, andcd build
. -
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, runconan install ..
if your conanfile.txt is in the parent directory. -
Run the build system,
cmake
, passing the directory containing your CMakeLists.txt, to create the Makefile. -
Run
cmake --build .
ormake
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.
@lkcole checkout this PR works for me let me know if any issues
rathod-sahaab/cpp-conan-template#2