Macros vs. functions in CMake

CMake is a build system that provides us with tools to configure and oversee software project builds. CMake utilizes macros and functions, which can be used to make CMake scripts. Although macros and functions share common features, their implementations approach differs. This Answer will delve into these differences.

Macros

In CMake, a macro is a block of code that can be reused throughout the CMake script. Macros are defined using the macro() command and invoked using the call() command. One of the key features of macros is that they allow for flexible code expansion, meaning the code within a macro can vary based on the arguments passed to it.

An example of a macro can be seen in the code below:

macro(my_macro arg1 arg2)
    message("Argument 1: ${arg1}")
    message("Argument 2: ${arg2}")
endmacro()

my_macro("Hello" "World")
Macro in CMake

This code can be explained as follows:

  • Line 1: This is the definition of the my_macro macro, which takes two arguments.

  • Lines 2–3: The message() method is used to print values to the terminal.

  • Line 4: This is the end of the macro definition.

  • Line 6: This calls our macro with the values Hello and World.

Function

Functions are similar to macros as they encapsulate a block of code for reuse. However, functions offer a more structured approach. These are defined using the function() command and invoked using the function name directly. Unlike macros, functions have their own scope and local variables, providing greater encapsulation.

An example of a function can be seen below:

function(my_function arg1 arg2)
    message("Argument 1: ${arg1}")
    message("Argument 2: ${arg2}")
endfunction()

my_function("Hello" "World")
Function in CMake

This code can be explained as follows:

  • Line 1: This is the definition of the my_function function, which takes two arguments arg1 and arg2.

  • Lines 2–3: The message() method is used to print values to the terminal.

  • Line 4: This is the end of the function definition.

  • Line 6: Calling our function with the values Hello and World.

As we can see in the codes above, they’re very similar. However, there are a few key differences we need to point out:

Feature

Function

Macro

Scope

Functions have their own scope, which means that variables declared within a function are local to only that function.

Macros don't have their own scope and work in the context of the caller.

Expansion Time

Functions are more predictable in their behaviour as they are expanded when they're defined.

Macros are expanded when it is called, which allows for dynamic code generation.

Arguments

Arguments are treated as variables.

Arguments are treated as strings and can be manipulated with string operations.

Therefore, we can infer the following points about when to use macros:

  • When dynamic code expansion based on the parameters is vital.

  • Simple code snippets that don’t require a local variable scope.

On most other occasions, functions are preferred.

Quiz

Test what you have learned so far.

1

What is one of the key features of macros in CMake?

A)

They have their own scope.

B)

They allow for flexible code expansion.

C)

They are defined using the function() command.

D)

They utilize local variables.

Question 1 of 30 attempted

Conclusion

In conclusion, CMake provides functions and macros as tools for code reuse in CMake scripts. They share some similarities, such as encapsulating code blocks for reuse, but their approach and behavior differ. Grasping these differences can help us write more efficient and logical code.


Free Resources

Copyright ©2024 Educative, Inc. All rights reserved