C++#

Buildsystem#

The meta buildsystem is CMake with the respective CMakeLists.txt. The basic CMakeLists.txt is provided by the copier template and should be adapted accordingly.

The C++ target standard is C++20, as configured in the CMakeLists.txt. The copier template provides vcpkg triplets which enforces this for dependencies.

Package Manager#

The package manager of choice is vcpkg.

vcpkg is installed by cloning the vcpkg repository. Create an environment variable with variable name VCPKG_ROOT and variable value path/to/vcpkg.

The copier template provides a vcpkg.json manifest and a vcpkg-configuration.json. The vcpkg manifest contains rudimentary information about your project. List your dependencies in the vcpkg.json dependencies array. To install dependencies run CMake. During the CMake configure step vcpkg will be invoked to provide them. Look out for a note how to add the missing dependencies to your targets within the CMakeLists.txt.

Note

You can search available dependencies on vcpkg.link (recommended) or the official vcpkg index.

vcpkg is hooked to CMake by a non-intrusive toolchain file:

if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
        CACHE STRING "")
endif()

If you need to use an actual toolchain file, you can provide it via VCPKG_CHAINLOAD_TOOLCHAIN_FILE.

Source Formatting#

The formatting is determined by the .clang-format, included in respective copier templates.

Warning

Changes are prohibited and the CI enforces compliance. Violation implies Pull Request rejection.

Compilation#

Before attempting to compile a project make sure to setup the package manager and buildsystem.

The project needs to be configured with CMake first. This can either be done with the provided presets or manually (in which case I recommend using ccmake or cmake-gui). The presets live within CMakePresets.json and can be supplemented with a CMakeUserPresets.json. The CMakePresets.json is included in all copier templates concerning C++. The presets can be used via the commandline like so

cd $project-root
# lists all available presets
cmake --list-presets
# configures the current project with the given preset
cmake --preset=x64-linux-gcc
# builds the previously configured project
cmake --build --preset=x64-linux-gcc
# usually there exists a release build-preset, too
cmake --build --preset=x64-linux-gcc-release

However, IDEs like Visual Studio and Visual Studio Code have integrated support for CMakePresets.json (see VS docs, VS Code docs and this blog post).

Warning

Note that if you do use the commandline, it is your responsibility to ensure that the target compiler is reachable via $PATH. On Windows you probably have to run these commands from a Visual Studio command prompt.