# Learn CMake's Scripting Language in 15 Minutes

DevFeed: [Learn CMake's Scripting Language in 15 Minutes](<https://devfeed.tech/articles/learn-cmake-s-scripting-language-in-15-minutes-21009.md>)

Original publisher: [Read original article](<https://preshing.com/20170522/learn-cmakes-scripting-language-in-15-minutes>)

Author: Jeff Preshing

Published: 2017-05-22T12:20:00Z

Content type: tutorial

Language: en

Sources: [Jeff Preshing](<https://devfeed.tech/sources/jeff-preshing.md>)

Topics: [CMake](<https://devfeed.tech/topics/cmake.md>), [Scripting](<https://devfeed.tech/topics/scripting.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Command-line interface](<https://devfeed.tech/topics/cli.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [cmake](<https://devfeed.tech/tags/cmake.md>), [command-line](<https://devfeed.tech/tags/command-line.md>), [guide](<https://devfeed.tech/tags/guide.md>), [scripting](<https://devfeed.tech/tags/scripting.md>), [syntax](<https://devfeed.tech/tags/syntax.md>)

## AI overview

A practical introduction to CMake's scripting language, covering its syntax and programming model through examples involving scripts, command-line execution, variables, variable references, and simulated data structures.

## Source excerpt

As explained in my previous post, every CMake-based project must contain a script named CMakeLists.txt. This script defines targets, but it can also do a lot of other things, such as finding third-party libraries or generating C++ header files. CMake scripts have a lot of flexibility. Every time you integrate an external library, and often when adding support for another platform, you'll need to edit the script. I spent a long time editing CMake scripts without really understanding the language, as the documentation is quite scattered, but eventually, things clicked. The goal of this post is to get you to the same point as quickly as possible. This post won't cover all of CMake's built-in commands, as there are hundreds, but it is a fairly complete guide to the syntax and programming model of the language. Hello World If you create a file named hello.txt with the following contents: message("Hello world!") # A message to print ...you can run it from the command line using cmake -P hello.txt. (The -P option runs the given script, but doesn't generate a build pipeline.) As expected, it prints "Hello world!". $ cmake -P hello.txt Hello world! All Variables Are Strings In CMake, every variable is a string. You can substitute a variable inside a string literal by surrounding it with ${}. This is called a variable reference. Modify hello.txt as follows: message("Hello ${NAME}!") # Substitute a variable into the message Now, if we define NAME on the cmake command line using the -D option, the script will use it: $ cmake -DNAME=Newman -P hello.txt Hello Newman! When a variable is undefined, it defaults to an empty string: $ cmake -P hello.txt Hello ! To define a variable inside a script, use the set command. The first argument is the name of the variable to assign, and the second argument is its value: set(THING "funk") message("We want the ${THING}!") Quotes around arguments are optional, as long as there are no spaces or variable references in the argument. For example, I c