I was recently asked what toolchains are, and how they relate to CMake. If you look up "toolchain" online, you'll find definitions that make complete sense to those who already know what they are, but sound vague and complicated to everyone else. So, here's a quick explanation...

What is a Toolchain?

This is best understood by example, so let's look at the GCC toolchain. When a developer wants to turn source code into a working program, he executes a tool called "Make." Make, in turn, calls the GCC compiler for each source file.

GCC runs the source file through multiple tools. First, it's processed by "cpp," the C pre-processor. This runs all the macros (the #includes and #defines). The pre-processed source code is then passed to cc1, the actual compiler. Cc1 converts the C source code into CPU-specific assembly code.

The assembly code is then passed to the next tool: "gas," the assembler. It converts the assembly code into machine code that the CPU can execute.

Once all C source code has been converted into machine code, the resulting "object files" are passed to "ld," the linker. This links the separate files into the final program.

If you look at the tools above, they form a chain, where the output from one tool becomes the input to the next. Here's the big picture (see below):

The GCC Toolchain

Where Does CMake Fit In?

CMake sits right at the top of the chain. It reads in its own build script(s) (CMakeLists.txt), and generates a build script for your chosen toolchain. It supports all the major C/C++ toolchains on all major operating systems (and even has a version for AmigaOS, albeit a bit dated at this point).

This is what makes CMake so powerful. One build script (plus source-code) can be used to build the same program on multiple platforms with whatever C/C++ compiler is best.

Want to learn how to do this (write software using C++ & CMake)? Check out my other cmake posts (link). Or, go to cmaketutorial.com (link).