C ++ with cross platform and dependencies

C ++ is still used not only for writing operating systems, games and drivers, but also for resource-intensive command line utilities. Meanwhile, competitors in this arena, such as Rust, offer a build system with a default dependency manager. For C ++, de facto, there is also a standard CMake build system, but how to connect external libraries without pain? Recall that for many advanced technologies there is something like a https://start.yourtechnology.io page that creates a basic standard project so as not to waste time on boilerplate configuration. This article discusses just such a pattern for creating C ++ projects with the vcpkg dependency manager.





Why vcpkg?

First, because of the desire to simplify the base project as much as possible and reduce the number of configuration files in it. For C ++, there is another advanced package manager Conan , but it requires adding a file conanfile.txt



, and vcpkg manages one standard one CMakeLists.txt



. Secondly, vcpkg is well established and has stable support from Microsoft.





1. Setting up the toolchain for work

First of all, we need CMake and the vcpkg dependency manager itself, you can install it through your favorite package manager (for example brew install vcpkg



), or build it manually from here .





2. Installing dependencies

Check if the required library is available

vcpkg search yourdepname





Install

vcpkg install yourdepname







, , , , vcpkg . , , :

vcpkg install catch2

vcpkg install cli11

vcpkg install fmt








, , vcpkg CMakeLists.txt







CMakeLists.txt



? , vcpkg:





cmake_minimum_required(VERSION 3.17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(proj)
file(GLOB proj_sources src/*.cpp)
add_executable(proj ${proj_sources})
find_package(fmt CONFIG REQUIRED)
target_link_libraries(proj PRIVATE fmt::fmt fmt::fmt-header-only)
find_package(CLI11 CONFIG REQUIRED)
target_link_libraries(proj PRIVATE CLI11::CLI11)

project(test)
#[[Changing the entry point for tests]]
list(FILTER proj_sources EXCLUDE REGEX ".*Main.cpp$")
file(GLOB test_sources test/*.cpp)
add_executable(test ${proj_sources} ${test_sources})
find_package(Catch2 CONFIG REQUIRED)
target_link_libraries(test PRIVATE Catch2::Catch2)
target_link_libraries(test PRIVATE CLI11::CLI11)
target_link_libraries(test PRIVATE fmt::fmt fmt::fmt-header-only)
      
      



3.

, CMake , ? IDE, CMake options vcpkg integrate install





IDE , CMake :





cmake `vcpkg integrate install | tail -1 | cut -d \"` -f2 -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" -B cmake-build-release
      
      



Windows, `` , vcpkg integrate install





, :

cmake --build cmake-build-release --target all









vcpkg install [...]



.



++ 21 . GitHub , . , "Use this template"  .

c C++ . !








All Articles