Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minimal CMake Example

This project is a minimal, self-contained CMake example. It demonstrates how to structure sources, resources, optional dependencies, unit tests, and packaging — all from a single CMakeLists.txt file.

It was created because the official CMake documentation tends to focus on peripheral concerns — such as generating configuration headers — rather than the practical essentials of setting up a real project. This example takes the opposite approach: compile the source cleanly, with no generated magic, and keep all build logic visible in one place.

A configuration header is something this project deliberately avoids. In my opinion, generating one makes the build unnecessarily complicated and harder for tools that work on source code. Similarly, one CMakeLists.txt at the root should be sufficient for most projects. Scattering build instructions across multiple subdirectories adds friction without adding clarity.

Note on Boost

Boost Filesystem/System are used as an optional example of an external dependency. If CMake cannot find Boost on your system, the project falls back to std::filesystem automatically. The build will succeed either way. You can also point CMake to a custom Boost installation using ccmake or cmake-gui.

Requirements

  • CMake (3.16 or newer)
  • A C++ compiler with C++17 support (AppleClang, Clang, GCC, or MSVC)

Optional Dependencies

  • Boost filesystem/system (optional)

    • If not found, this project falls back to std::filesystem.
  • GoogleTest (optional, only needed to build and run unit tests)

    macOS (Homebrew)

    brew install googletest

    Ubuntu / Debian

    sudo apt install libgtest-dev

    Windows (vcpkg)

    vcpkg install gtest
    cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=<path\to\vcpkg>\scripts\buildsystems\vcpkg.cmake

    Alternatively, with vcpkg integrated:

    vcpkg integrate install

Build

Configure and build from the repository root:

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

Run

Run the sample executable:

./build/example

Test

If GoogleTest is installed and found by CMake, the unit_tests target is built and tests are registered with CTest.

Run tests with:

ctest --test-dir build --output-on-failure

If GoogleTest is not installed, the project still configures and builds, but no tests are discovered.

Package

Create distributable packages with CPack:

cpack --config build/CPackConfig.cmake

About

This solution is a minimal CMake example, that covers sources resources dependencies and packaging.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages