What is a Toolchain, & How Does CMake Fit Into It?
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):
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).
2 Comments
Hans de Ruiter 20/05/2024 10:06am (7 months ago)
I'm glad this blog post is helpful. CMake toolchain files are covered in The CMake Tutorial (https://cmaketutorial.com). You usually only need them for cross-compiling (e.g., to another OS, or when compiling C/C++ code to the Web with emscripten).
M 18/05/2024 4:20pm (7 months ago)
"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..."
as if CMake is usually "taught" by "those who already know"... which doesn't help the people who don't know. Nonetheless, this elucidation was appreciated. I just hoped that it followed into the topic of "toolchain files", since any "documentation" doesn't elaborate on what they are exactly, yet they're often touted as something to use. It makes it very confusing to try and define what a "toolchain file" is, especially when it's not clearly shown up front where a "toolchain file" might be a special set of options (or whatever) or just a file that's run before something else that happens. Thanks for taking on the task of actually considering how to teach people who want to learn how to use cmake.