What is a CMake generator?

A CMake Generator is responsible for writing the input files for a native build system.

means that CMake prepares build scripts for a native build system when no generator is specified. In Linux the default build system is Make and its input file are makefiles, which are then interpreted and a build is executed accordingly. Before the first execution of CMake build scripts do not exist.

C++ source files (or any other source files) are not input files to a build system. Build system scripts specify how to handle source file in order to produce binary executables.


Maybe a picture is worth a thousand words.

cmake-underlying

This question also puzzled me for a while, which led me to come here. I searched for some information and made this picture based on my knowledge. If there is any mistake in the picture, please correct me to avoid misleading.


What's a generator?

To understand what a generator is, we need to first look at what is a build system. CMake doesn't compile or link any source files. It used a generator to create configuration files for a build system. The build system uses those files to compile and link source code files.

So what's a build system?

A build system is a broad term that groups together a set of tools used to generally compile and link source code, but it can also include auxiliary tools used during a build process.

For example, in a multi-stage build system, one executable might be built to be used in the build process of another build.

Depending on the tool chain used on a system, CMake will generate multiple files and folders to allow the building of the source files referenced in the CMakeLists.txt and supporting .cmake files.

Sometimes multiple build systems may be installed on a computer, like for Windows you could have a Visual Studio and MinGW build system. CMake allows you to specify which if these build systems to generate configuration files for.

CMake includes a number of Command-Line, IDE, and Extra generators.

Command-Line Build Tool Generators

These generators are for command-line build tools, like Make and Ninja. The chosen tool chain must be configured prior to generating the build system with CMake.

The following are supported(**):

  • Borland Makefiles
  • MSYS Makefiles
  • MinGW Makefiles
  • NMake Makefiles
  • NMake Makefiles JOM
  • Ninja
  • Unix Makefiles
  • Watcom WMake

IDE Build Tool Generators

These generators are for Integrated Development Environments that include their own compiler. Examples are Visual Studio and Xcode which include a compiler natively.

The following are supported(**):

  • Visual Studio 6
  • Visual Studio 7
  • Visual Studio 7 .NET 2003
  • Visual Studio 8 2005
  • Visual Studio 9 2008
  • Visual Studio 10 2010
  • Visual Studio 11 2012
  • Visual Studio 12 2013
  • Visual Studio 14 2015
  • Visual Studio 15 2017
  • Visual Studio 16 2019
  • Green Hills MULTI
  • Xcode

Extra Generators

These are generators that create a configuration to work with an alternative IDE tool and must be included with either an IDE or Command-Line generator.

The following are supported(**):

  • CodeBlocks
  • CodeLite
  • Eclipse CDT4
  • KDevelop3 (Unsupported after v3.10.3)
  • Kate
  • Sublime Text 2

If I have a set of C++ files in my project, are these the input files?

Yes, they are some of the input files. For a make build system you also have a MakeFile. For Visual Studio you have a solution file (.sln). With both systems there are additional files needed that CMake knows how to create given a proper CMakeLists.txt file.

If I'm using Linux, what is my native build system by default? Make?

Generally, yes, but other build systems could be setup like Ninja.

Why do the input files have to be written by the generator if they already exist?

Some source files may already exist, but CMake has the ability to generate header and source files. Also as mentioned above, there are configuration files that must be generated that depend on the source files supplied in the CMakeLists.txt file.

** According to the documentation for CMake Version 3.9 & 3.15