A C++ project template designed for quick startup with best practices pre-configured. Based on cpp-best-practices/cmake_template.
When configured as the top-level project, this template automatically enables:
- Security & Safety: AddressSanitizer, UB Sanitizer, Stack Protector, Fortify Source.
- Quality Assurance: Warnings as errors, Static analysis (clang-tidy, cppcheck).
- Dependency Management: Via CMake
FetchContent. - Testing: Unit testing with Catch2, Fuzz testing with LibFuzzer.
- Tooling: Code coverage (gcovr), ccache support.
Click the "Use this template" button on GitHub to create a new repository, then clone it.
To customize the project name (default: myproject), search and replace myproject with your desired name in the following files:
CMakeLists.txtconfigured_files/config.hpp.ininclude/myproject/sample_library.hpp
The CMake build system handles the rest automatically.
This project supports CMake Presets (requires CMake >= 3.21) to simplify the configuration and build process.
List available configuration presets:
cmake --list-presetsConfigure and build (example for GCC Debug on Unix-like systems):
cmake --preset unixlike-gcc-debug
cmake --build --preset unixlike-gcc-debugRun tests:
ctest --preset test-unixlike-gcc-debugNote: If you renamed the project, replace
myprojectwith your new name in the flags below.
cmake -S . -B build
cmake --build buildcd build
ctest --output-on-failureRequires gcovr.
cmake -S . -B build -Dmyproject_ENABLE_COVERAGE=ON
cmake --build build --target coverage-reportThe report will be generated in build/coverage.
Requires a compiler with LibFuzzer support (e.g., Clang).
cmake -S . -B build -Dmyproject_BUILD_FUZZ_TESTS=ON
cmake --build build
cd build
ctest -R fuzz_testerYou can control features via CMake options (-D<Option>=ON/OFF):
| Option | Description | Default |
|---|---|---|
myproject_ENABLE_HARDENING |
Enable security hardening (stack protection, etc.) | ON |
myproject_WARNINGS_AS_ERRORS |
Treat compiler warnings as errors | ON* |
myproject_ENABLE_CLANG_TIDY |
Enable static analysis with clang-tidy | ON* |
myproject_ENABLE_CPPCHECK |
Enable static analysis with cppcheck | ON* |
myproject_ENABLE_CACHE |
Use ccache if available to speed up rebuilds | ON* |
myproject_ENABLE_COVERAGE |
Enable code coverage generation | OFF |
myproject_BUILD_FUZZ_TESTS |
Build fuzzing executables | ON |
*Default is ON only when the project is the top-level project (not a sub-module).
Derived from the cmake_template.