Modern C++20 project template with CMake, Google Test, and Ninja.
Required:
- CMake 3.25+ (
brew install cmake) - C++20 compiler (GCC 10+, Clang 10+, MSVC 2019+)
- Ninja (
brew install ninja)
Optional (targets degrade gracefully if missing):
- clang-format -- for
make format/make format-check(included with Xcode) - clang-tidy -- for
make tidy/make tidy-fix(brew install llvmon macOS) - lcov + genhtml -- for
make coverage - pre-commit -- for git hooks (
pip install pre-commit)
Google Test is fetched automatically by CMake (no install needed).
make build # generate + build
make test # run all tests
make run # run the applicationCMakeLists.txt # main cmake config
Makefile # convenience wrapper
cmake/ # cmake modules (ClangTools, CodeCoverage)
src/
common/include/ # shared headers
foo/ # library module
include/ src/ tests/
app/ # application
src/
| Target | Description | Requires |
|---|---|---|
make build |
Generate + build | cmake, ninja |
make rebuild |
Clean + build | cmake, ninja |
make test |
Build + run tests via ctest | ctest |
make run |
Build + run application | |
make clean |
Remove build directory | |
make format |
Format code with clang-format | clang-format |
make format-check |
Check formatting without modifying | clang-format |
make tidy |
Run clang-tidy static analysis | clang-tidy |
make tidy-fix |
Run clang-tidy with auto-fix | clang-tidy |
make coverage |
Generate code coverage report | lcov, genhtml |
make xcode |
Generate + open Xcode project | macOS |
Off by default. Enable via CMake options:
cmake -B .build -DENABLE_ASAN=ON # AddressSanitizer (memory errors)
cmake -B .build -DENABLE_TSAN=ON # ThreadSanitizer (data races)
cmake -B .build -DENABLE_UBSAN=ON # UndefinedBehaviorSanitizer- Create the directory structure:
src/mymodule/
CMakeLists.txt
include/mymodule.h
src/mymodule.cpp
tests/
CMakeLists.txt
tests.cpp
-
Add
add_subdirectory(mymodule)tosrc/CMakeLists.txt. -
Update
app_binariesin the Makefile if the module produces an executable.
Update the clearly marked PROJECT SETTINGS sections:
- CMakeLists.txt --
project()name and version - Makefile --
app_binariespaths
Everything else (build targets, cmake modules, test infrastructure) is generic boilerplate.
MIT