cmake-cpp icon indicating copy to clipboard operation
cmake-cpp copied to clipboard

Modern CMake C++ Sample

Github-CI:
Build Status Build Status Build Status

Build Status Build Status Build Status

Build Status Build Status Build Status Build Status Build Status Build Status

Introduction

This is an example of how to create a Modern CMake C++ Project.

This project should run on GNU/Linux, MacOS and Windows.

Requirement

You'll need:

  • "CMake >= 3.16".

Codemap

The project layout is as follow:

  • CMakeLists.txt Top-level for CMake based build.

  • cmake Subsidiary CMake files.

  • ci Root directory for continuous integration.

  • Foo Root directory for Foo library.

    • CMakeLists.txt for Foo.
    • include public folder.
      • foo
        • Foo.hpp
    • src private folder.
      • src/Foo.cpp
  • Bar Root directory for Bar library.

    • CMakeLists.txt for Bar.
    • include public folder.
      • bar
        • Bar.hpp
    • src private folder.
      • src/Bar.cpp
  • FooBar Root directory for FooBar library.

    • CMakeLists.txt for FooBar.
    • include public folder.
      • foobar
        • FooBar.hpp
    • src private folder.
      • src/FooBar.cpp
  • FooBarApp Root directory for FooBarApp executable.

    • CMakeLists.txt for FooBarApp.
    • src private folder.
      • src/main.cpp

Dependencies

To complexify a little, the CMake project is composed of three libraries (Foo, Bar and FooBar) with the following dependencies:

Foo:
Bar:
FooBar: PUBLIC Foo PRIVATE Bar
FooBarApp: PRIVATE FooBar

note: Since Foo is a public dependency of FooBar, then FooBarApp will see Foo inlude directories

Build

To build the C++ project, as usual:

cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release

Appendices

Few links on the subject...

Resources

Project layout:

  • The Pitchfork Layout Revision 1 (cxx-pflR1)

CMake:

  • https://llvm.org/docs/CMakePrimer.html
  • https://cliutils.gitlab.io/modern-cmake/
  • https://cgold.readthedocs.io/en/latest/

Misc

Image has been generated using plantuml:

plantuml -Tsvg docs/{file}.dot

So you can find the dot source files in ci/docs.

License

Apache 2. See the LICENSE file for details.

Disclaimer

This is not an official Google product, it is just code that happens to be owned by Google.