...

/

Conditional Blocks

Conditional Blocks

Let's learn about logical operators, conditional blocks, and control structures.

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:

    ...