CGold Documentation
Release 0.1
Ruslan Baratov
Feb 17, 2019
1 Overview
1.1 What CMake can do .
1.2 What can’t be done with CMake .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
Contents
3
3
14
19
19
27
38
39
40
60
2 First step
Compiler .
CMake Installation .
.
.
.
.
2.1
.
.
2.2 Native build tool
.
2.3
.
.
.
2.4 Minimal example .
.
2.5 Generate native tool files .
Build and run executable .
2.6
.
.
.
.
.
.
.
.
.
3 Tutorials
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
CMake stages .
.
3.1
.
.
3.2 Out-of-source build .
.
3.3 Workflow .
.
.
.
3.4 Version and policies
.
Project declaration .
3.5
.
.
3.6 Variables .
.
.
CMake listfiles .
.
3.7
.
Control structures
.
3.8
.
.
Executables .
.
3.9
.
.
.
.
3.10 Tests
.
.
.
.
3.11 Libraries
.
.
3.12 Pseudo targets
.
.
.
3.13 Collecting sources .
.
3.14 Usage requirements
.
.
3.15 Build types .
.
.
.
3.16 configure_file .
.
.
.
3.17 Install .
.
.
3.18 Toolchain .
.
.
3.19 Generator expressions .
.
3.20 Properties .
3.21 Packing .
.
.
3.22 Continuous integration .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
4 Platforms
4.1
iOS .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
67
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
67
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
72
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
74
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
78
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
85
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
91
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
233
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
i
4.2 Android .
5 Generators
5.1 Ninja .
.
.
.
6 Compilers
7 Contacts
Public .
.
Private .
.
.
7.1
.
7.2
7.3 Hire .
.
7.4 Donations .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
253
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
255
257
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
8 Rejected
ExternalProject_Add .
.
FindXXX.cmake .
.
.
.
.
.
.
.
.
target_compile_features .
8.1
.
8.2
.
8.3 macro .
.
.
8.4 Object libraries .
.
8.5
.
8.6 write_compiler_detection_header
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
259
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
9 Glossary
.
-B .
.
-H .
-S .
.
CMake .
.
.
.
Binary tree .
.
.
.
.
9.1
.
.
.
.
.
9.2
.
.
.
.
.
9.3
.
.
.
.
.
9.4
.
.
.
.
.
.
9.5 Git
.
.
.
9.6 Hunter
.
.
.
.
.
9.7 Native build tool
.
.
.
.
.
9.8 VCS .
.
.
.
9.9
.
.
.
.
.
9.10 Cache variables .
.
.
9.11 CMake module .
.
.
.
9.12 CMake variables .
.
.
9.13 CMakeCache.txt
.
.
9.14 CMakeLists.txt .
.
.
.
9.15 Developer Command Prompt
.
9.16 Listfile .
.
.
.
9.17 Multi-configuration generator
.
9.18 One Definition Rule (ODR)
.
.
9.19 Single-configuration generator .
9.20 Source tree .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
267
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
.
.
ii
CGold Documentation, Release 0.1
Warning: THIS DOCUMENT IS UNDER CONSTRUCTION! WORK IN PROGRESS!
Welcome to CGold!
This guide will show you how to use CMake and will help you to write elegant, correct and scalable projects. We’ll
start from the simple cases and add more features one by one. This tutorial covers only part of the CMake capabilities -
some topics are skipped intentionally in favor of better modern approaches 1. Document designed to be a good tutorial
for the very beginners but touches some aspects in which advanced developers may be interested too. Look at this
document as a skeleton/starting point for further CMake learning.
Enjoy!
1 See rejected section for list with detailed description
Contents
1
CGold Documentation, Release 0.1
2
Contents
CHAPTER 1
Overview
1.1 What CMake can do
CMake is a meta build system. It can generate real native build tool files from abstracted text configuration. Usually
such code lives in CMakeLists.txt files.
What does it mean and how it can be useful?
1.1.1 Cross-platform development
Let’s assume you have some cross-platform project with C++ code shared along different platforms/IDEs. Say you
use Visual Studio on Windows, Xcode on OSX and Makefile for Linux:
What you will do if you want to add new bar.cpp source file? You have to add it to every tool you use:
3
CGold Documentation, Release 0.1
To keep the environment consistent you have to do the similar update several times. And the most important thing is
that you have to do it manually (arrow marked with a red color on the diagram in this case). Of course such approach
is error prone and not flexible. CMake solve this design flaw by adding extra step to development process. You can
describe your project in CMakeLists.txt file and use CMake to generate tools you currently interested in using
cross-platform CMake code:
4
Chapter 1. Overview