

With functions it’s not so simple: function(setBar value) A simple set command will define a variable in the caller’s scope macro(setFoo value) With macros, this is obvious and is often done implicitly. Returning is done via an output parameter. Neither functions nor macros in CMake have a dedicated return channel.

This makes macros easier to use when returning a value is necessary. Scope and returning valuesĪs I already mentioned functions introduce a new scope, whereas macros don’t. The little extra effort is worth it, as it could prevent some debugging sessions as the project grows. I’d suggest sticking with functions whenever possible. Accidentally overwriting existing variables is also likely. I’d advise against it for the same reason one would avoid macros in C/C++ – it’s best not to pollute the calling scope with whatever variables are defined within the macro’s body. Before we move on to more interesting examples let me touch on one important point.īecause macros don’t introduce new scope they’re often chosen over functions for their ease of use – it’s much more intuitive to “return” a value by defining or mutating a variable in the caller’s scope than it is to use a function.

CMAKE FUNCTION CODE
Once defined, both can be called just like builtin CMake commands: myFunction("hello" "functions")Įxecuting this code results in the expected output: $ cmake -S. In the macro’s case they are string-substitutions, but for day-to-day use this shouldn’t make much difference to the user – they’re still accessed the same way within the macro’s body. The parameters foo and bar are regular variables in case of the function. The most important difference between the two is that functions introduce a new scope, whereas macros don’t – their body is essentially copy-pasted into the call site.įunctions and macros can be defined as follows: function(myFunction foo bar) Functions and Macros basicsĪt a first glance, CMake functions and macros behave exactly as one would expect, having experience with any of the C/C++ family of languages. Once the basics are covered I then move on to demonstrating the uses of cmake_parse_arguments. In this post I’ll discuss the basic use of functions and macros in CMake. Due to CMake language scope rules, this requires some special handling and is rather unintuitive, which often causes people to relegate to macros altogether. This is especially true for functions that are expected to return a value. However, they might be somewhat inconvenient to use at first. It enables users to define both functions and macros allowing for encapsulation of repetitive tasks. The CMake docs splits expressions into Informational, Logical, and Output.Every scripting language needs to provide some facilities for code reuse. They act as if they are evaluated at build/install time, though actually they are evaluated for each build configuration. You can mix it with the positional arguments listed above any remaining arguments (therefore optional positional arguments) are in COMPLEX_PREFIX_UNPARSED_ARGUMENTS.
CMAKE FUNCTION FREE
If you look at the official page, you'll see a slightly different method using set to avoid explicitly writing the semicolons in the list feel free to use the structure you like best. Inside the function after this call, you'll find: COMPLEX_PREFIX_SINGLE = TRUEĬOMPLEX_PREFIX_MULTI_VALUES = "some other values"
CMAKE FUNCTION SERIES
There are a series of all caps keywords you can use inside an if statement, and you can often refer to variables by either directly by name or using the $Ĭomplex(SINGLE ONE_VALUE value MULTI_VALUES some other values) CMake has an if statement, though over the years it has become rather complex.
