调用CMake函数:参数数量

sam*_*sam 1 cmake

有一个名为的函数myfunc定义为

function (myfunc var1 var2 var3)
...
endfunction()
Run Code Online (Sandbox Code Playgroud)

然后,我看到一个函数调用

myfunc(custom hahaha platform ALL
        COMMAND echo "hello world"
        SOURCES ${some_var} )
Run Code Online (Sandbox Code Playgroud)

我的问题是

该函数myfunc有3个变量。在上面的函数调用中,这三个变量是什么?此外,哪有额外命令COMMANDSOURCES函数调用内?

gor*_*kic 6

3 variables will be the first 3 arguments.

If your function was defined as follows:

function (myfunc var1 var2 var3)
  message ("var1: ${var1}")
  message ("var2: ${var2}")
  message ("var3: ${var3}")
  message ("number of arguments sent to function: ${ARGC}")
  message ("all function arguments:               ${ARGV}")
  message ("all arguments beyond defined:         ${ARGN}") 
endfunction()
Run Code Online (Sandbox Code Playgroud)

after calling it as you stated:

set (some_var "some var")
myfunc(custom hahaha platform ALL
        COMMAND echo "hello world"
        SOURCES ${some_var} )
Run Code Online (Sandbox Code Playgroud)

the outuput will be:

var1: custom
var2: hahaha
var3: platform
number of arguments sent to function: 9
all function arguments:               custom;hahaha;platform;ALL;COMMAND;echo;hello world;SOURCES;some var
all arguments beyond defined:         ALL;COMMAND;echo;hello world;SOURCES;some var
Run Code Online (Sandbox Code Playgroud)

so you have called your function with 9 arguments, that are referenced with ${ARGV}, all arguments that are not defined can also be referenced using variable ${ARGN}.

Note that when calling function, ALL, COMMAND, and SOURCES are just arguments to the function, nothing else.

In the end, here is the full documentation about cmake functions and arguments