Search⌘ K
AI Features

Command Definitions: Functions

Explore how to declare functions in CMake, handle argument scopes, and use command definitions effectively. Understand the procedural paradigm and how to structure your code with entry-point macros to improve readability and maintainability.

Functions in CMake

To declare a command as a function, follow this syntax:

function(<name> [<argument>…])
<commands>
endfunction()
Function syntax

A function requires a name and optionally accepts a list of names of expected arguments. If a function call passes more arguments than were declared, the excess arguments will be interpreted as anonymous arguments and stored in the ARGN variable.

As mentioned before, functions open their own scope. We can call set(), providing one of the named arguments of the function, and any change will be local to the function (unless PARENT_SCOPE is specified).

Functions follow the rules of the call ...