55

I read the documentation.

It says:

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

What exactly does that mean?

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

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

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

4 Answers 4

64

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(**):

Makefile Generators

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

Ninja Generators

  • Ninja
  • Ninja Multi-Config

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
  • Visual Studio 17 2022
  • 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 & 3.25

2
  • Can i check from cmd to get list of available generators for visual studio on windows ? Commented Nov 16, 2020 at 16:11
  • 2
    @NikolaLukic Yes, just run Cmake -h, the help output will list which generators are available.
    – jmstoker
    Commented Nov 24, 2020 at 18:14
43

Maybe a picture is worth a thousand words.

cmake-underlying

10
  • If there is any mistake in the picture, please correct me to avoiding misleading. Nice picture. I have considered making one myself. Your representation is closer to my mental image but the key concept in my mental image is that it is a tree with branches and as you descend down the tree each branch at that level is independent of a sibling branch. A branch can be dependent upon its parent but is independent of its sibling, so the child rectangles spanning more than one parent should be independent, e.g. compiler. Continued
    – Guy Coder
    Commented Aug 9, 2022 at 13:59
  • Same for generators, each generator is and creates a new independent branch. So the single large rectangle for generator should be four rectangles for each specific generator. The one thing that I find sorely missing in many but not all great intros to CMake is that they do not show the history of how to go from a C++ source code file to an executable (exe, obj, a, DLL, etc.). They just jump in using CMake. If you start with the history it tracks nicely with the leaves of the tree and as you evolve building you move up the tree with a CMakeList.txt file as the parent for a source file. :)
    – Guy Coder
    Commented Aug 9, 2022 at 14:05
  • this picture seems un-accessable now.
    – Kevin Chan
    Commented Oct 25, 2022 at 3:15
  • @GuyCoder Thank you. Yeah, you are right, the generator also specifies the build kits. I agree that a tree with branches would be more specific and accurate to describe the relationship between concepts. I should clarify that the different layers indicate the abstraction of the building process and that is how CMake achieves cross-platform features. However, the vertical rectangles don't intend to show accurate relationships. Commented Nov 12, 2022 at 13:06
  • 1
    @KevinChan How is it now? The picture is accessible in my browser Commented Nov 12, 2022 at 13:09
7

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.

0
1

As far as I know, the standard native build system in Unix is GNU Make (gmake) known as "make".

The Google guys/gals also released a different tool called Ninja.

Not the answer you're looking for? Browse other questions tagged or ask your own question.