49

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
batman
  • 5,022
  • 11
  • 52
  • 82

4 Answers4

60

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

jmstoker
  • 3,315
  • 22
  • 36
38

Maybe a picture is worth a thousand words.

cmake-underlying

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Charles Sun
  • 550
  • 4
  • 7
  • `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 Aug 09 '22 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 Aug 09 '22 at 14:05
  • this picture seems un-accessable now. – Kevin Chan Oct 25 '22 at 03: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. – Charles Sun Nov 12 '22 at 13:06
  • @KevinChan How is it now? The picture is accessible in my browser – Charles Sun Nov 12 '22 at 13:09
  • thank you that is accessible now :) @CharlesSun – Kevin Chan Nov 14 '22 at 09:37
  • What is "tooset"? Do you mean "toolset"? – Peter Mortensen Jan 29 '23 at 17:16
  • The relations between the elements could be clearer. It isn't clear what the relations are between the blocks on the right, both vertically and horizontally (for example, *"compiles"*, *"generates"*, *"builds"*, *"contains"*, *invokes* (on the command line), etc.). There should probably also be a *direction* on the relations. They could even be ***named*** relations so the reader doesn't have to guess what the relations are. – Peter Mortensen Jan 29 '23 at 17:23
  • And/or there should be more words... – Peter Mortensen Jan 29 '23 at 17:25
  • How to debug a Ninja generated project? For example, By using `cmake -GNinja -DCMAKE_BUILD_TYPE=DEBUG ..` I generated a project. But can I debug it? If I can, which IDEshould I choose? – Lake_Lagunita Jun 21 '23 at 02:18
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.

Adam Kosiorek
  • 1,438
  • 1
  • 13
  • 17
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bench
  • 11
  • 2