This is a template to start C++ project using common Google libraries.
- C++17 by default
- Formatting via clang-format
(run
scripts/clang-format.sh
to reformat everything) - Clang/GCC sanitizers
- Benchmarks via Benchmark library
- Tests via GoogleTest library
- Logging via Google Logging Library library
- Abseil library
sudo apt-get install cmake
# Using Homebrew
brew install --cask cmake
# Using MacPorts
sudo port install cmake +gui
There are several build types:
-DCMAKE_BUILD_TYPE=Debug
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_BUILD_TYPE=RelWithDebInfo
-DCMAKE_BUILD_TYPE=MinSizeRel
There are multiple generators including:
-G "Unix Makefiles"
-G Ninja
# Run cmake
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -G "Unix Makefiles"
# Run make via cmake
cmake --build build
# Or run make directly
make -C build VERBOSE=1
# Run compiled binary
GLOG_logtostderr=1 build/src/hexdump --filename build/src/hexdump
# Run cmake
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -G Ninja
# Run ninja via cmake
cmake --build build
# Or run ninja directly
ninja -C build
# Run compiled binary
GLOG_logtostderr=1 build/src/hexdump --filename build/src/hexdump
Tests are enabled by default:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=OFF -DRUNTIME_SANITIZERS=address,undefined -G Ninja
cmake --build build
ctest -V --test-dir build
Test receives TEST_SRCDIR
environment variable which points to
${CMAKE_SOURCE_DIR}/tests
. Specify it manually if running test
binary directly:
TEST_SRCDIR=$(pwd)/tests build/tests/library_test
Disable tests by passing -DBUILD_TESTING=OFF
to cmake
command.
Check more about writing tests in GoogleTest Primer.
Benchmarks are enabled by default:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -G Ninja
cmake --build build
build/benchmarks/library_benchmark
Disable benchmarks by passing -DBUILD_BENCHMARKS=OFF
to cmake
command.
Presets
are defined in CMakePresets.json
.
# debug-make
cmake --preset debug-make
cmake --build --preset debug-make
ctest --preset debug-make
# debug-ninja
cmake --preset debug-ninja
cmake --build --preset debug-ninja
ctest --preset debug-ninja
# release-make
cmake --preset release-make
cmake --build --preset release-make
# release-ninja
cmake --preset release-ninja
cmake --build --preset release-ninja
# check variables
cmake --preset release-ninja -N