[CMake entry and advanced (11)] CMake macro definition and function (with code)

        Cmake provides a way to define macros. The function function in cmake is the same as the macro definition macro to some extent. They both create a piece of code with a name that can be called later and pass parameters. Macros are defined via the macro() command as follows:

macro(<name> [arg1 [arg2 [arg3 ...]]])
    COMMAND1(ARGS ...)
    COMMAND2(ARGS ...)
    ...
endmacro(<name>)

        The writable in endmacro brackets may not be written, if written, it must be consistent with the macro brackets. The parameter name indicates the name of the macro definition. In the macro definition, the variables ARGVX (X is a number), ARGC, ARGV, and ARGN introduced earlier can also be used, so these are also internal variables of the macro definition, as shown below:

# macro 宏定义测试
macro(XYZ arg1 arg2)
    message("ARGC: ${ARGC}")
    message("ARGV: ${ARGV}")
    message("ARGN: ${ARGN}")
    message("ARGV0: ${ARGV0}")
    message("ARGV1: ${ARGV1}")

    # 循环打印出各个参数
    set(i 0)
    foreach(loop ${ARGV})
        message("arg${i}: " ${loop})
        math(EXPR i "${i} + 1")
    endforeach()
endmacro()

# 使用宏
XYZ(A B C D E)

        The source code print information is as follows:

         From the definition, they seem to be exactly the same. Macros and functions are indeed similar, but there are still differences. For example, macro parameters and values ​​such as ARGV, ARGC, and ARGN are not variables in the usual CMake sense. They are string replacements. Like the C language preprocessor does for macros, so you won't be able to use:

if(ARGV1) # ARGV1 is not a variable
if(DEFINED ARGV2) # ARGV2 is not a variable
if(ARGC GREATER 2) # ARGC is not a variable
foreach(loop_var IN LISTS ARGN) # ARGN is not a variable

        Because in the macro definition, the parameters of the macro and the values ​​​​such as ARGC, ARGV, ARGN, etc. are not variables, they are string replacements, that is, when cmake executes the macro definition, it will first combine the parameters of the macro with ARGC, ARGV , ARGN and other values ​​are replaced by strings, and then this macro is executed, which is actually like a preprocessing step in C language, which is different from functions. Let's test it out:

# macro 宏
macro(abc arg1 arg2)
    if(DEFINED ARGC)
        message(true)
    else()
        message(false)
    endif()
endmacro()

# function 函数
function(xyz arg1 arg2)
    if(DEFINED ARGC)
        message(true)
    else()
        message(false)
    endif()
endfunction()

# 调用宏
abc(A B C D)

# 调用函数
xyz(A B C D)

         In the above code, we define a macro abc and a function xyz. The codes of both of them are the same. They both use if() internally to judge whether ARGC is a variable. If it is true, it will print true, if it is not, it will print false; The macro abc and the function xyz will be called respectively, and the printed information is as follows:

        So from the printed information, we can see that in the macro definition, ARGC is not a variable. In fact, ARGC will be replaced before the macro is executed, as shown below:

if(DEFINED 4)
    message(true)
else()
    message(false)
endif()

        Replace ARGC with 4 (since we actually passed 4 arguments). Of course, in addition to this, there are other differences between functions and macro definitions in cmake. For example, functions have their own scope, while macro definitions have no concept of scope.

Guess you like

Origin blog.csdn.net/cj_lsk/article/details/131300325