我的代码:
function(my_function)
cmake_parse_arguments(MY_FUNCTION "" "LIST1;LIST2" "" ${ARGN})
message("List 1: ${MY_FUNCTION_LIST1}")
message("List 2: ${MY_FUNCTION_LIST2}")
# Alternatively, you can access the lists directly without parsing
foreach(item IN LISTS MY_FUNCTION_LIST1)
message("Item in List 1: ${item}")
endforeach()
foreach(item IN LISTS MY_FUNCTION_LIST2)
message("Item in List 2: ${item}")
endforeach()
endfunction()
set(my_list1 "item1;item2;item3")
set(my_list2 "itemA;itemB;itemC")
my_function(LIST1 "${my_list1}" LIST2 "${my_list2}")
我希望将完整的 my_list1 和完整的 my_list2 传递给 my_function,但消息输出
List 1: item1
List 2: itemA
Item in List 1: item1
Item in List 2: itemA
它只获取列表第一项,如何将完整列表传递给函数
您已在调用
LIST1
时将 LIST2
和 cmake_parse_arguments
列为 单参数参数。
只需更改这一行:
cmake_parse_arguments(MY_FUNCTION "" "LIST1;LIST2" "" ${ARGN})
对此:
cmake_parse_arguments(MY_FUNCTION "" "" "LIST1;LIST2" ${ARGN})
输出:
List 1: item1;item2;item3
List 2: itemA;itemB;itemC
Item in List 1: item1
Item in List 1: item2
Item in List 1: item3
Item in List 2: itemA
Item in List 2: itemB
Item in List 2: itemC
或者,您也可以使用
PARSE_ARGV
的 cmake_parse_arguments
形式,如下所示:
cmake_parse_arguments(PARSE_ARGV 0 MY_FUNCTION "" "LIST1;LIST2" "")
这会产生与上面相同的输出。
请参阅此处的文档:https://cmake.org/cmake/help/latest/command/cmake_parse_arguments.html