Skip to content

Quick-start C++ project template with CMake, Sanitizers, Static Analysis, and Unit Testing pre-configured.

License

Notifications You must be signed in to change notification settings

myuverr/cpp-project-template

Repository files navigation

C++ Project Template

C++ CMake License

A C++ project template designed for quick startup with best practices pre-configured. Based on cpp-best-practices/cmake_template.

Key Features

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.

Getting Started

1. Create Repository

Click the "Use this template" button on GitHub to create a new repository, then clone it.

2. Rename Project

To customize the project name (default: myproject), search and replace myproject with your desired name in the following files:

  • CMakeLists.txt
  • configured_files/config.hpp.in
  • include/myproject/sample_library.hpp

The CMake build system handles the rest automatically.

Building & Testing

This project supports CMake Presets (requires CMake >= 3.21) to simplify the configuration and build process.

Using Presets (Recommended)

List available configuration presets:

cmake --list-presets

Configure and build (example for GCC Debug on Unix-like systems):

cmake --preset unixlike-gcc-debug
cmake --build --preset unixlike-gcc-debug

Run tests:

ctest --preset test-unixlike-gcc-debug

Manual Build

Note: If you renamed the project, replace myproject with your new name in the flags below.

cmake -S . -B build
cmake --build build

Manual Testing

cd build
ctest --output-on-failure

Advanced Usage

Code Coverage

Requires gcovr.

cmake -S . -B build -Dmyproject_ENABLE_COVERAGE=ON
cmake --build build --target coverage-report

The report will be generated in build/coverage.

Fuzz Testing

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_tester

Configuration Options

You 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).

Attribution

Derived from the cmake_template.

About

Quick-start C++ project template with CMake, Sanitizers, Static Analysis, and Unit Testing pre-configured.

Topics

Resources

License

Stars

Watchers

Forks