Search⌘ K
AI Features

Conditional Blocks

Explore how to implement and use conditional blocks with if, elseif, and else commands in CMake. Understand syntax rules, logical operators, and evaluation nuances to control script behavior during build system generation efficiently.

The CMake Language wouldn't be complete without control structures! Like everything else, they are provided in the form of a command, and they come in three categories:

  • Conditional blocks

  • Loops

  • Command definitions

Control structures are executed in scripts and during buildsystem generation for projects.

if() conditional block

The only conditional block supported in CMake is the humble if() command. All conditional blocks have to be closed with an endif() command, and they may have any number of elseif() commands and one optional else() command in this order:

if(<condition>)
<commands>
elseif(<condition>) # optional block, can be repeated
<commands>
else() # optional block
<commands>
endif()
Syntax of multiple elseif sections

As in many other imperative languages, the if()-endif() block controls which sets of commands will be executed:

  • If the ...