Useful CMake Examples
Go to file
2025-05-09 02:18:07 +00:00
01-basic Merge branch 'master' into IrvingW-patch-1 2021-09-14 20:20:45 +01:00
02-sub-projects 更新 02-sub-projects/A-basic/CMakeLists.txt 2025-05-09 02:18:07 +00:00
03-code-generation spelling fixes 2020-01-17 19:48:14 -05:00
04-static-analysis fix linking 2020-04-23 23:13:29 +08:00
05-unit-testing remove duplicate 2020-04-24 11:57:24 +08:00
06-installer Fix typo. teh -> the 2018-04-15 03:16:14 -04:00
07-package-management spelling fixes 2020-01-17 19:48:14 -05:00
dockerfiles spelling fixes 2020-01-17 19:48:14 -05:00
.gitignore add description of ExternalProject_Add 2019-04-04 22:37:57 +01:00
.travis.yml Update to minimum CMake 3.5 2018-03-18 17:23:57 +00:00
cmake-examples.sublime-project add initial folder structure and the first basic examples 2015-11-21 18:51:01 +00:00
LICENSE Initial commit 2015-11-21 11:03:11 +00:00
README.adoc Changes: 2019-12-17 15:24:29 +13:00
test.sh add conan using imported targets 2019-05-05 18:57:49 +01:00

= CMake Examples
:toc:
:toc-placement!:

toc::[]

# Introduction

https://cmake.org/[CMake] is a cross-platform open-source meta-build system which
can build, test and package software. It can be used to support multiple native build environments including
make, Apple's xcode and Microsoft Visual Studio.

This repository includes some example modern CMake configurations which I have picked up
when exploring it's usage for various projects. The examples are laid out in a tutorial like format.
The first examples are very basic and slowly increase in complexity drawing on previous examples to show
more complex use cases.

These examples have been tested on Ubuntu 16.04 but should work under any Linux system that supports CMake v3.5+.

This branch works with the CMake version 3.5 onwards. 

* For examples that use CMake version 2.x see the branch link:https://github.com/ttroy50/cmake-examples/tree/v2-style-includes[v2-style-includes].
* For examples that use CMake version 3.0 see the branch link:https://github.com/ttroy50/cmake-examples/tree/v3.0-minimum[v3.0-minimum]

image:https://travis-ci.org/ttroy50/cmake-examples.svg?branch=master["Build Status", link="https://travis-ci.org/ttroy50/cmake-examples"]

# Requirements

The basic requirements for most examples are:

* CMake v3.5+
* A c++ compiler (defaults to gcc)
* make

## Installation on Ubuntu

The easiest way to install the above on Ubuntu is as follows

[source,bash]
----
$ sudo apt-get install build-essential
$ sudo apt-get install cmake
----

Some specific examples may require other tools including:

* http://www.boost.org/[boost]

  $ sudo apt-get install libboost-all-dev

* https://github.com/google/protobuf[protobuf]

  $ sudo apt-get install libprotobuf-dev
  $ sudo apt-get install protobuf-compiler

* http://cppcheck.sourceforge.net/[cppcheck]

  $ sudo apt-get install cppcheck

* http://clang.llvm.org/[clang]

  $ sudo apt-get install clang-3.6

* https://ninja-build.org/[ninja]

  $ sudo apt-get install ninja-build

* link:https://conan.io[conan]

  $ sudo apt-get install python3 python3-pip
  $ sudo pip3 install conan

## Docker

Docker containers with all requirements and various versions of CMake are generated to help make testing the examples easier. These are available from the docker hub repository link:https://hub.docker.com/r/matrim/cmake-examples/[matrim/cmake-examples].

To build the full set of cmake-examples test cases you can run:

[source,bash]
----
docker run -it matrim/cmake-examples:3.5.1
cd ~ 
git clone https://github.com/ttroy50/cmake-examples.git code
cd code
./test.sh
----

For more details on build and running the docker containers link:here[dockerfiles].

# Other Links

There are many CMake tutorials and examples online. The list below includes links
to some of these which I have found helpful in my CMake journey.

  * https://web.archive.org/web/20160314094326/https://www.kdab.com/~stephen/moderncmake.pdf[Modern CMake Slides]
  * https://rix0r.nl/blog/2015/08/13/cmake-guide/[rix0r Modern CMake Blog]
  * https://cmake.org/cmake-tutorial/[Official CMake Tutorial]
  * https://gitlab.kitware.com/cmake/community/wikis/home[Official CMake Wiki]
  * https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/Useful-Variables[CMake Useful Variables]
  * http://derekmolloy.ie/hello-world-introductions-to-cmake/[Derek Molloy - Intro to CMake]
  * http://techminded.net/blog/modular-c-projects-with-cmake.html[Modular C++ Projects]
  * https://web.archive.org/web/20190320121339/http://voices.canonical.com/jussi.pakkanen/2013/03/26/a-list-of-common-cmake-antipatterns/[Common CMake Anti-Patterns]
  * http://baptiste-wicht.com/posts/2014/04/install-use-clang-static-analyzer-cmake.html[Using clang static analyser with CMake]
  * https://cmake.org/pipermail/cmake/2011-April/043709.html[Static Analysis with CDash] - Includes some info about using CppCheck with CMake
  * https://samthursfield.wordpress.com/2015/10/20/some-cmake-tips/[CMake Tips]
  * https://www.johnlamp.net/cmake-tutorial.html[John Lamp - CMake Tutorial]
  * link:https://docs.conan.io[Conan Documentation]