# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a b c d) # 创建列表变量"a;b;c;d" list (LENGTH list_test length) message (">>> LENGTH: ${length}")
<element index>为列表元素的索引,从0开始编号,索引0的元素为列表中的第一个元素;索引也可以是负数,-1表示列表的最后一个元素,-2表示列表倒数第二个元素,以此类推。注意:当索引(不管是正还是负)超过列表的长度,运行会报错(list index: XX out of range)。 <output variable>为新创建的变量,存储指定索引元素的返回结果,也是一个列表。
1 2 3 4 5 6
# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a b c d) # 创建列表变量"a;b;c;d" list (GET list_test 0 1 -1 -2 list_new) message (">>> GET: ${list_new}")
# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a b c d) # 创建列表变量"a;b;c;d" list (JOIN list_test -G- list_new) message (">>> JOIN: ${list_new}")
# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a b c d) # 创建列表变量"a;b;c;d" list (FIND list_test d list_index) message (">>> FIND 'd': ${list_index}") list (FIND list_test e list_index) message (">>> FIND 'e': ${list_index}")
1 2 3
# 输出 >>> FIND 'd': 3 >>> FIND 'e': -1
APPEND 将元素追加到列表
1
list(APPEND <list> [<element>...])
此命令会改变原列表的值。
1 2 3 4 5 6
# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a b c d) # 创建列表变量"a;b;c;d" list (APPEND list_test 1 2 3 4) message (">>> APPEND: ${list_test}")
# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a b c d 1234) # 创建列表变量"a;b;c;d;1;2;3;4" message (">>> the LIST is: ${list_test}") list (FILTER list_test INCLUDE REGEX [a-z]) message (">>> FILTER: ${list_test}") list (FILTER list_test EXCLUDE REGEX [a-z]) message (">>> FILTER: ${list_test}")
1 2 3 4
# 输出 >>> the LIST is: a;b;c;d;1;2;3;4 >>> FILTER: a;b;c;d >>> FILTER:
# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a b c d) # 创建列表变量"a;b;c;d" list (POP_FRONT list_test) message (">>> POP_FRONT: ${list_test}") list (POP_FRONT list_test outvar1 outvar2 outvar3 outvar4) message (">>> POP_FRONT: ${outvar1}、${outvar2}、${outvar3}、${outvar4}")
1 2 3
# 输出 >>> POP_FRONT: b;c;d >>> POP_FRONT: b、c、d、
PREPEND 将元素插入到列表的0索引位置
1
list(PREPEND <list> [<element>...])
如果待插入的元素是多个,则相当于把多个元素整体“平移”到原列表0索引的位置。
1 2 3 4 5 6 7 8
# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a b c d) # 创建列表变量"a;b;c;d" list (PREPEND list_test z) message (">>> PREPEND: ${list_test}") list (PREPEND list_test p q r s t) message (">>> POP_FRONT: ${list_test}")
# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a a b c c d) # 创建列表变量"a;a;b;c;c;d" list (REMOVE_ITEM list_test a) message (">>> REMOVE_ITEM: ${list_test}") list (REMOVE_ITEM list_test b e) message (">>> REMOVE_ITEM: ${list_test}")
# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a b c d e f) # 创建列表变量"a;b;c;d;e;f" list (REMOVE_AT list_test 0 -1) message (">>> REMOVE_AT: ${list_test}") list (REMOVE_AT list_test 1 1 1 1) message (">>> REMOVE_AT: ${list_test}")
1 2 3
# 输出 >>> REMOVE_AT: b;c;d;e >>> REMOVE_AT: b;d;e
REMOVE_DUPLICATES
1
list(REMOVE_DUPLICATES <list>)
移除列表中的重复元素
1 2 3 4 5 6 7 8 9 10 11 12
# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a a a b b b c c c d d d) # 创建列表变量"a;a;a;b;b;b;c;c;c;d;d;d;" list (REMOVE_DUPLICATES list_test) message (">>> REMOVE_DUPLICATES: ${list_test}") set (list_test a a a a) list (REMOVE_DUPLICATES list_test) message (">>> REMOVE_DUPLICATES: ${list_test}") set (list_test a b c a d a e a f) # 多个元素重复,只保留第一个 list (REMOVE_DUPLICATES list_test) message (">>> REMOVE_DUPLICATES: ${list_test}")
# CMakeLists.txt cmake_minimum_required (VERSION 3.12.2) project (list_cmd_test) set (list_test a b c d) list (TRANSFORM list_test APPEND B OUTPUT_VARIABLE list_test_out) list (TRANSFORM list_test APPEND B) message (">>> TRANSFORM-APPEND: ${list_test} --- ${list_test_out}") set (list_test a b c d) list (TRANSFORM list_test PREPEND F OUTPUT_VARIABLE list_test_out) list (TRANSFORM list_test PREPEND F) message (">>> TRANSFORM-PREPEND: ${list_test} --- ${list_test_out}")