This repository is a copy-and-adapt sample for a small C++20 application. Rename the sample identifiers and replace the greeting code when starting a real project; it is not intended to be used unchanged.
src/main.cppdefines the main application,sample_cpp_project.- All other headers and
.cppfiles undersrc/form the STATIC librarysample_cpp_project_lib. - Each
.cppfile found recursively undertools/becomes a separate executable and linkssample_cpp_project_lib. tools/CMakeLists.txtdiscovers and configures those tool executables.third_party/include/CLI11.hppcontains the vendored CLI11 v2.6.2 header used by the main application.CMakePresets.jsonprovides Linux/GCC-compatible and Windows/MSVC debug presets using Ninja..vscode.sample/contains optional Linux/GDB and Windows/MSVC editor tasks and launch settings.
The project library is linked into the final executables. It is not a runtime file that must be distributed beside them.
From the project root, configure and build the debug preset:
cmake --preset linux-debug
cmake --build --preset linux-debugRun the application:
./build/bin/sample_cpp_project --name AdaThe Linux and Windows presets both use build/. When switching platforms in the same checkout,
delete build/ before configuring the other preset because an existing CMake build tree cannot be
reused with a different platform or compiler.
On Windows, run the following from a regular Command Prompt:
build_vs2022.batThe script locates Visual Studio 2022, initializes its x64 compiler environment, configures the
windows-msvc-debug Ninja preset, and builds build\bin\sample_cpp_project.exe. The CMake targets
use the static MSVC runtime, CLI11 is header-only, and sample_cpp_project_lib is a static library,
so no project DLL or separate project-library file is needed beside the executable. Normal Windows
system components are still provided by the target machine.
Copy the sample directory before opening the project in VS Code:
cp -r .vscode.sample .vscodeOn Windows Command Prompt, the equivalent is:
xcopy .vscode.sample .vscode\ /E /IThe copied configuration supplies configure, build, and debug entries for Linux with GDB and Windows
with the Visual Studio debugger. If the application target or output path changes, update the
program entries in .vscode/launch.json and the related preset names in .vscode/tasks.json.
Tools are enabled by default. Every recursively discovered tools/*.cpp file must define exactly
one main(). Relative paths are flattened with underscores for internal CMake target names, and
paths that flatten to the same target name are rejected during configuration. Output paths retain
the source layout: tools/sample_tool.cpp produces build/tools/sample_tool, while
tools/admin/check.cpp produces build/tools/admin/check (with .exe on Windows).
Disable all tool executables while configuring with:
cmake --preset linux-debug -DSAMPLE_CPP_PROJECT_BUILD_TOOLS=OFFUse the same option with windows-msvc-debug on Windows.
Run the configured formatters from the project root:
find src -type f \( -name '*.cpp' -o -name '*.h' \) -exec clang-format -i {} +
find tools -type f -name '*.cpp' -exec clang-format -i {} +
cmake-format -i CMakeLists.txt tools/CMakeLists.txt
prettier --write README.md CMakePresets.json .vscode.sample/*.json- Search for
sample_cpp_projectand replace the CMake project name, application target,sample_cpp_project_lib,sample_cpp_project_configure_target, tool-target prefix, source namespace, batch output name, and VS Code launch paths as appropriate. - Search for the uppercase prefix
SAMPLE_CPP_PROJECT. RenameSAMPLE_CPP_PROJECT_BUILD_TOOLS,SAMPLE_CPP_PROJECT_LIBRARY_SOURCES, andSAMPLE_CPP_PROJECT_TOOL_SOURCESconsistently. - Replace
src/greeting.h,src/greeting.cpp,make_greeting, and thesample_cpp_projectnamespace with the real library API and implementation. Keepsrc/main.cppoutside the static library. - Update the CLI description,
-n,--nameoption, default value, and application behavior insrc/main.cpp. Update or removetools/sample_tool.cppfor the tools the project needs. - Rename the
linux-debugandwindows-msvc-debugpresets if desired, then update matching names inbuild_vs2022.batand.vscode.sample/tasks.json. - If output directories or executable names change, update
build_vs2022.batand bothprogrampaths in.vscode.sample/launch.json. - Keep
third_party/include/CLI11.hppand its include path if CLI11 is still used. If thesrc/,tools/, orthird_party/include/layout changes, update the corresponding paths in the CMake files.