有一个CMAKE_ARGC
变量包含传递给 CMake 的变量数量(除以空格),并且CMAKE_ARGV0
, CMAKE_ARGV1
, ... 包含实际值。
对于 C++ 语言来说,第一个(零)变量保存你调用的命令是很常见的(cmake
在这种情况下),所以我们需要除CMAKE_ARGV0
. 那么让我们做一个简单的循环:
set(PASSED_PARAMETERS "") # it will contain all params string
set(ARG_NUM 1) # current index, starting with 1 (0 ignored)
# you can subtract something from that if you want to ignore some last variables
# using "${CMAKE_ARGC}-1" for example
math(EXPR ARGC_COUNT "${CMAKE_ARGC}")
while(ARG_NUM LESS ARGC_COUNT)
# making current arg named "CMAKE_ARGV" + "CURRENT_INDEX"
# in CMake like in Bash it's easy
set(CURRENT_ARG ${CMAKE_ARGV${ARG_NUM}})
message(STATUS "Doing whatever with ${CURRENT_ARG}")
# adding current param to the list
set(PASSED_PARAMETERS ${PASSED_PARAMETERS} ${CURRENT_ARG})
math(EXPR ARG_NUM "${ARG_NUM}+1") # incrementing current index
endwhile()
(回答我自己的问题,在 SO 中没有找到类似的东西,也许它会帮助某人)