ArticleslgStudy

computer science

CMake

CMake is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand CMake rather than just read about it. In short: CMake is a free, cross-platform, software development tool for building applications via compiler-independent instructions. It also can automate testing, packaging and installation.

CMake — main illustration
CMake — illustration

Key takeaways

  • CMake belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect CMake to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of CMake from memory before moving on to harder problems.

Reference excerpt

CMake is a free, cross-platform, software development tool for building applications via compiler-independent instructions. It also can automate testing, packaging and installation. It runs on a variety of platforms and supports many programming languages. As a meta-build tool, CMake configures native build tools which in turn build the codebase. CMake generates configuration files for other build tools based on CMake-specific configuration files. The other tools are responsible for more directly building, using the generated files. A single set of CMake-specific configuration files can be used to build a codebase using the native build tools of multiple platforms. Notable native build tools supported by CMake include: Make, Qt Creator, Ninja, Android Studio, Xcode, and Visual Studio. CMake is distributed as free and open-source software under a permissive BSD-3-Clause license.

History Initial development began in 1999 at Kitware with funding from the United States National Library of Medicine as part of the Visible Human Project. CMake was first released in 2000. CMake was developed to support building the Insight Segmentation and Registration Toolkit (ITK) for multiple platforms. Stated goals included addressing weaknesses while maintaining strengths of contemporary tools such as autoconf and libtool, and to align with state of the art build technology of the time: configure scripts and Make files for Unix platforms, and Visual Studio project files for Windows. CMake was inspired by multiple contemporary tools. pcmaker – developed by Ken Martin and others to support building the Visualization Toolkit (VTK) – converted Unix Make files into NMake files for building on Windows. gmake supported Unix and Windows compilers, but its design led to issues that were hard to resolve. Both tools were working examples of a build tool that supported both Unix and Windows, but they suffered from a serious flaw: they required Windows developers to use the command line even though many prefer to use an integrated development environment (IDE) such as Visual Studio. CMake was to provide similar cross-platform support but to better satisfy the preferences of the developers on each platform. The design goals of the first version included:

Depend only on host C++ compiler; no other third-party tools or libraries required Generate Visual Studio project files (as well as Unix files) Support building targets: program, static library, shared library Run build-time code generators Support separate directory trees for source vs. build files Support host computer capability introspection Support automatic dependency scanning of C/C++ header files All features must work consistently and equally well on all supported platforms For various reasons, CMake developers chose to develop a scripting language for CMake instead of using Tcl – a popular language for building at the time. Use of Tcl would have then added a dependency to the host machine which is counter to the goal of no dependencies other than a compiler. Also, Tcl was not well supported on Windows and some Unix systems at the time of initial development. Subsequent development and improvements were fueled by the incorporation of CMake into developers’ own systems, including the VXL Project, the CABLE features added by Brad King, and GE Corporate R&D for support of DART (which is deprecated since 3.7.). Additional features were created when VTK transitioned to CMake for its build environment and for supporting ParaView. Version 3.0 was released in June 2014. It has been described as the beginning of "Modern CMake". Experts now advise to avoid variables in favor of targets and properties. The commands add_compile_options(), include_directories(), link_directories(), link_libraries() that were at the core of CMake 2 should now be replaced by target-specific commands.

Name Although CMake is primarily associated with C and C++ projects, CMake developer Brad King commented in 2013, "Of course the 'C' in CMake stands for 'cross-platform'".

Features

Generators and IDE support CMake can generate project files for IDEs like Microsoft Visual Studio, Xcode, Eclipse CDT and build scripts for MSBuild or NMake on Windows; Unix Make on Unix-like platforms such as Linux, macOS, and Cygwin; and Ninja on both Windows and Unix-like platforms by specifying generator for a platform-specific build tool. By default, CMake automatically determines default generator for the host environment it runs on. Command line option -G can be used to specify alternative generator. E.g. -G Unix Makefiles forces CMake to create build scripts for make. CMake does not support custom generators without modifying the CMake implementation. None-the-less, the CMake source code could be modified to include a custom generator.

Build targets CMake supports building executables, libraries (e.g. libxyz, xyz.dll etc.), object file libraries and pseudo-targets (including aliases). CMake can produce object files that can be linked against by executable binaries/libraries, avoiding dynamic (run-time) linking and using static (compile-time) linking instead. This enables flexibility in configuration of various optimizations. Target generation can be configured via target properties. With older versions, this was done via CMAKE_-prefixed global variables, but this approach is deprecated.

Hierarchical configuration CMake configuration files can be structured according the hierarchical structure of the source code; the source tree. A CMakeLists.txt in a root source directory serves as the root of the configuration. It may include sub-directories which each contain a CMakeLists.txt. Repeating this, results in a hierarchical structure of configuration that follows the structure of the source code.

Separate build tree CMake can store generated files (both by CMake and the native build tools) in a directory tree that is separate from the source tree. This enables multiple builds from the same source tree since each has non-overlapping file system space. This may be leveraged to build different or even incompatible configurations such as for different platforms. This also simplifies file management by allowing removing generated files by deleting a single directory tree instead of removing multiple files and directories throughout the source tree. This tends to prevent accidentally deleting source files or accidentally adding generated files to source control.

… excerpt ends here. Continue reading the full article.

Illustrations

CMake illustration
CMake illustration

Worked examples

Example 1 — a first encounter with CMake

Start with the simplest possible case. Write down what CMake claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to CMake before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about CMake ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of CMake

In research
CMake appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses CMake in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
CMake is common in secondary-school and first-year university syllabi. It links to neighbouring topics 2000 software, Build automation, Compiling tools, so understanding it makes those chapters shorter.
In everyday life
Look for CMake outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “CMake” →

How to study CMake in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what CMake means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain CMake out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is CMake in simple terms?

CMake is a free, cross-platform, software development tool for building applications via compiler-independent instructions. It also can automate testing, packaging and installation.

Why does CMake matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study CMake?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on CMake.

Tags

  • 2000 software
  • Build automation
  • Compiling tools
  • Free software programmed in C
  • Free software programmed in C++
  • Software using the BSD license

Keep exploring