Skip to content

A template CMake C++ project based on popular Google open source libraries.

License

Notifications You must be signed in to change notification settings

dmitriykovalev/cpp-project-template

Repository files navigation

CMake C++ Project Template

This is a template to start C++ project using common Google libraries.

Install

Linux

sudo apt-get install cmake

macOS

# Using Homebrew
brew install --cask cmake
# Using MacPorts
sudo port install cmake +gui

Build

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

Debug + Unix Makefiles

# 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

Release + Ninja

# 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

Test

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.

Benchmark

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

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

Resources

About

A template CMake C++ project based on popular Google open source libraries.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published