Dependencies#
This page will guide you through the process of managing dependencies in your C++ project. We will use the CMake
build system to manage dependencies.
Dependencies Sources#
There are several sources can be used to manage dependencies in your C++ project. Here are available ones:
System packages (Not recommended): these are installed on your system and can be easily found using
apt-get
,yum
,pacman
, etc.Vcpkg packages (Recommended): there are many packages available on
vcpkg
.Conan packages: there are also packages available on
conan
.CPM packages: there are also packages available on
CPM
.
Using Vcpkg#
Vcpkg is a C++ package manager that provides a simple way to install and integrate third-party libraries. It is available for Windows, Linux, and MacOS. It is the default package manager of the generated project. See the following links to learn how to manage dependencies using Vcpkg:
Using Conan#
Conan is an another C++ package manager integrated with CMake scripts in the generated project.
Note
Conan is default disabled in the generated project. To enable it, you need to add -d use_conan=true
to the copier command.
copier copy gh:serious-scaffold/ss-cpp /path/to/project -d use_conan=true
The above command will generate a cmake module file cmake/ConfigureConanDependencies.cmake
, which presents an example of how to use Conan to manage dependencies.
#[[
Use conan to add dependencies to the project.
conan is a CMake wrapper for conan C and C++ package manager.
See https://github.com/conan-io/cmake-conan.
Example:
conan_cmake_configure(REQUIRES fmt/6.1.2
GENERATORS cmake_find_package)
conan_cmake_autodetect(settings)
conan_cmake_install(PATH_OR_REFERENCE .
BUILD missing
REMOTE conancenter
SETTINGS ${settings})
find_package(fmt)
add_executable(main main.cpp)
target_link_libraries(main fmt::fmt)
Note:
Use cmake-conan/conan will break up the project dependency management of vcpkg. Best practice
to use this when the project would not be built as a shared library to be referred by
other vcpkg projects.
]]
find_package(cmake-conan REQUIRED)
include(cmake-conan/conan)
message(STATUS "conan is configuring extra deps...")
# Add extra deps here
Using CPM#
CPM is CMake’s missing package manager. A small CMake script for setup-free, cross-platform, reproducible dependency management. And it is also integrated with CMake scripts in the generated project.
Note
CPM is default disabled in the generated project. To enable it, you need to add -d use_cpm=true
to the copier command.
copier copy gh:serious-scaffold/ss-cpp /path/to/project -d use_cpm=true
The above command will generate a cmake module file cmake/ConfigureCPMDependencies.cmake
, which presents an example of how to use CPM to manage dependencies.
#[[
Use CPM to add dependencies to the project.
CPM is CMake's missing package manager. A small CMake script for setup-free,
cross-platform, reproducible dependency management.
See https://github.com/cpm-cmake/CPM.cmake.
Example:
# add executable
add_executable(main main.cpp)
# add dependencies using cpm module
CPMAddPackage("gh:fmtlib/fmt#7.1.3")
CPMAddPackage("gh:nlohmann/json@3.10.5")
CPMAddPackage("gh:catchorg/Catch2@3.2.1")
# link dependencies
target_link_libraries(main fmt::fmt nlohmann_json::nlohmann_json Catch2::Catch2WithMain)
Note:
Use cmake-cpm/CPM will break up the project dependency management of vcpkg. Best practice
to use this when the project would not be built as a shared library to be referred by
other vcpkg projects.
]]
find_package(cmake-cpm REQUIRED)
include(cmake-cpm/CPM)
message(STATUS "CPM is configuring extra deps...")
# Add extra deps here