Skip to content

Adrien4193/CppTemplate

Repository files navigation

C++ template project

My C++ template project (Windows only for now).

Requirements

Building

If you don't use vcpkg, make sure all CMake dependencies listed in vcpkg.json are available in PATH.

If you use vcpkg, it will fetch the dependencies automatically when configuring CMake if you provide the correct toolchain file (see below).

See CMakePresets.json for available presets or run cmake --list-presets.

First setup some CMake variables (for example windows debug with vcpkg):

export $CMAKE_TOOLCHAIN_FILE=path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
export $CMAKE_CXX_COMPILER=g++
export $CMAKE_BUILD_TYPE=Debug

Or:

$Env:CMAKE_TOOLCHAIN_FILE = "path/to/vcpkg/scripts/buildsystems/vcpkg.cmake"
$Env:CMAKE_CXX_COMPILER = "cl"
$Env:CMAKE_BUILD_TYPE = "Debug"

Then run CMake:

cmake . --preset windows
cmake --build --preset windows

Testing

ctest --preset windows

Packaging

cpack --preset windows

Workflows

cmake --workflow --preset windows

VSCode

Example settings.json to have:

  • A custom terminal with Visual Studio Developer PowerShell.
  • Visual studio CMake plugin able to find Visual Studio compile tools.
  • Test suites nicely displayed as a tree in the test explorer.
  • The correct debug configuration used from launch.json when a test is debugged.
{
    "terminal.integrated.profiles.windows": {
        "Developer PowerShell": {
            "source": "PowerShell",
            "args": [
                "-NoExit",
                "-Command",
                "& \"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\Tools\\Launch-VsDevShell.ps1\" -Arch amd64"
            ]
        }
    },
    "terminal.integrated.defaultProfile.windows": "Developer PowerShell",
    "cmake.useVsDeveloperEnvironment": "always",
    "cmake.ctest.testSuiteDelimiter": "\\.",
    "cmake.ctest.debugLaunchTarget": "(ctest) Launch"
}

Example tasks.json to build with CMake using Ctrl+Shift+B:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cmake",
            "label": "CMake: build",
            "command": "build",
            "preset": "${command:cmake.activeBuildPresetName}",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [],
            "detail": "CMake template build task"
        }
    ]
}

Example launch.json to debug apps and tests:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug application",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${command:cmake.launchTargetPath}",
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal"
        },
        {
            "name": "(ctest) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${cmake.testProgram}",
            "cwd": "${workspaceFolder}",
            "args": [
                "${cmake.testArgs}"
            ]
        }
    ]
}

User presets

For convenience, it is easier to set toolchain file, build type or compiler in a CMakeUserPresets.json at the root of the repo:

{
    "version": 10,
    "configurePresets": [
        {
            "name": "user",
            "hidden": true,
            "inherits": "windows",
            "toolchainFile": "${sourceDir}/../vcpkg/scripts/buildsystems/vcpkg.cmake",
            "cacheVariables": {
                "CMAKE_CXX_COMPILER": "cl"
            }
        },
        {
            "name": "debug",
            "inherits": "user",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            }
        },
        {
            "name": "release",
            "inherits": "user",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release"
            }
        }
    ],
    "buildPresets": [
        {
            "name": "debug",
            "inherits": "windows",
            "configurePreset": "debug"
        },
        {
            "name": "release",
            "inherits": "windows",
            "configurePreset": "release"
        }
    ],
    "testPresets": [
        {
            "name": "debug",
            "inherits": "windows",
            "configurePreset": "debug"
        },
        {
            "name": "release",
            "inherits": "windows",
            "configurePreset": "release"
        }
    ],
    "packagePresets": [
        {
            "name": "debug",
            "inherits": "windows",
            "configurePreset": "debug"
        },
        {
            "name": "release",
            "inherits": "windows",
            "configurePreset": "release"
        }
    ],
    "workflowPresets": [
        {
            "name": "debug",
            "steps": [
                {
                    "type": "configure",
                    "name": "debug"
                },
                {
                    "type": "build",
                    "name": "debug"
                },
                {
                    "type": "test",
                    "name": "debug"
                },
                {
                    "type": "package",
                    "name": "debug"
                }
            ]
        },
        {
            "name": "release",
            "steps": [
                {
                    "type": "configure",
                    "name": "release"
                },
                {
                    "type": "build",
                    "name": "release"
                },
                {
                    "type": "test",
                    "name": "release"
                },
                {
                    "type": "package",
                    "name": "release"
                }
            ]
        }
    ]
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published