Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops makefile或CMake文件中列表的并行迭代_Loops_Makefile_Cmake - Fatal编程技术网

Loops makefile或CMake文件中列表的并行迭代

Loops makefile或CMake文件中列表的并行迭代,loops,makefile,cmake,Loops,Makefile,Cmake,有没有办法在makefile或CMake文件中并行循环多个列表 我想在CMake中执行以下操作,但AFAICT不支持此语法: set(a_values a0 a1 a2) set(b_values b0 b1 b2) foreach(a in a_values b in b_values) do_something_with(a b) endforeach(a b) 这将执行: do_something_with(a0 b0) do_something_with(a1 b1) do_som

有没有办法在makefile或CMake文件中并行循环多个列表

我想在CMake中执行以下操作,但AFAICT不支持此语法:

set(a_values a0 a1 a2)
set(b_values b0 b1 b2)
foreach(a in a_values b in b_values)
  do_something_with(a b)
endforeach(a b)
这将执行:

do_something_with(a0 b0)
do_something_with(a1 b1)
do_something_with(a2 b2)
我会接受CMake或Make的答案,尽管CMake是首选。谢谢

给你:

set(list1 1 2 3 4 5)
set(list2 6 7 8 9 0)

list(LENGTH list1 len1)
math(EXPR len2 "${len1} - 1")

foreach(val RANGE ${len2})
  list(GET list1 ${val} val1)
  list(GET list2 ${val} val2)
  message(STATUS "${val1}  ${val2}")
endforeach()

从CMake 3.17开始,
foreach()
循环支持同时迭代两个(或更多)列表的选项:

set(a_values a0 a1 a2)
set(b_values b0 b1 b2)
foreach(a b IN ZIP_LISTS a_values b_values)
    message("${a} ${b}")
endforeach()
这张照片是:

a0 b0
a1 b1
a2 b2

make
中,您可以使用将两个列表处理为单列表来实现这一点:

include gmtt/gmtt.mk

# declare the lists as tables with one column
list_a := 1 a0 a1 a2 a3 a4 a5
list_b := 1 b0 b1 b2

# note: list_b is shorter and will be filled up with a default value
joined_list := $(call join-tbl,$(list_a),$(list_b), /*nil*/)

$(info $(joined_list))

# Apply a function (simply output "<tuple(,)>") on each table row, i.e. tuple
$(info $(call map-tbl,$(joined_list),<tuple($$1,$$2)>))
包括gmtt/gmtt.mk
#将列表声明为具有一列的表
列表a:=1 a0 a1 a2 a3 a4 a5
列表b:=1 b0 b1 b2
#注意:列表_b较短,将用默认值填充
加入列表:=$(调用加入tbl,$(列表a),$(列表b),/*nil*/)
$(信息$(已加入列表))
#对每个表行(即元组)应用函数(仅输出“”)
$(信息$(呼叫地图待定,$(已加入列表),)
输出:

2 a0 b0 a1 b1 a2 b2 a3 /*nil*/ a4 /*nil*/ a5 /*nil*/
<tuple(a0,b0)><tuple(a1,b1)><tuple(a2,b2)><tuple(a3,/*nil*/)><tuple(a4,/*nil*/)><tuple(a5,/*nil*/)>
2 a0 b0 a1 b1 a2 b2 a3/*nil*/a4/*nil*/a5/*nil*/

如果列表为空,则此操作将失败。你必须添加额外的条件来检查这应该是一个答案了。