+
Put your CMakeLists.txt
under version control
+
All CMake-related files will evolve together with your codebase. It’s a good idea to put them under version control. On the contrary, any of the generated native build-system files, e.g. Makefile
-s, should not be version-controlled.
+
+
+
The command-line interface to CMake
+
Let us get acquainted with the CMake and especially its command-line interface.
+
We can get help at any time with the command
+
+
This will output quite a number of options to your screen. We can analyze the last few lines first:
+
Generators
+
+The following generators are available on this platform (* marks default):
+* Unix Makefiles = Generates standard UNIX makefiles.
+ Green Hills MULTI = Generates Green Hills MULTI files.
+ Ninja = Generates build.ninja files.
+ Ninja Multi-Config = Generates build-<Config>.ninja files.
+ Watcom WMake = Generates Watcom WMake makefiles.
+ CodeBlocks - Ninja = Generates CodeBlocks project files.
+ CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
+ CodeLite - Ninja = Generates CodeLite project files.
+ CodeLite - Unix Makefiles = Generates CodeLite project files.
+ Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
+ Sublime Text 2 - Unix Makefiles = Generates Sublime Text 2 project files.
+ Kate - Ninja = Generates Kate project files.
+ Kate - Unix Makefiles = Generates Kate project files.
+ Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
+ Eclipse CDT4 - Unix Makefiles = Generates Eclipse CDT 4.0 project files.
+
+
+
In CMake terminology, the native build scripts and build tools are called generators. On any particular platform, the list will show which native build tools can be used through CMake. They can either be “plain”, such as Makefile
-s or Ninja, or IDE-like projects.
+
The -S
switch specifies which source directory CMake should scan: this is the folder containing the root CMakeLists.txt
, i.e., the one containing the |project| command. By default, CMake will allow in-source builds, i.e. storing build artifacts alongside source files. This is not good practice: you should always keep build artifacts from sources separate. Fortunately, the -B
switch helps with that, as it is used to give where to store build artifacts, including the generated build system. This is the minimal invocation of cmake
:
+
+
To switch to another generator, we will use the -G
switch:
+
$ cmake -S. -Bbuild -GNinja
+
+
+
Options to be used at build-system generation are passed with the -D
switch. For example, to change compilers:
+
$ cmake -S. -Bbuild -GNinja -DCMAKE_CXX_COMPILER=clang++
+
+
+
Finally, you can access to the full CMake manual with:
+
+
You can also inquire about a specific module, command or variable:
+
$ cmake --help-variable CMAKE_GENERATOR
+
+
+
+