【cmake】list

命令list

说明:

Reading
  list(LENGTH <list> <out-var>)
  list(GET <list> <element index> [<index> ...] <out-var>)
  list(JOIN <list> <glue> <out-var>)
  list(SUBLIST <list> <begin> <length> <out-var>)

Search
  list(FIND <list> <value> <out-var>)

Modification
  list(APPEND <list> [<element>...])
  list(FILTER <list> {INCLUDE | EXCLUDE} REGEX <regex>)
  list(INSERT <list> <index> [<element>...])
  list(POP_BACK <list> [<out-var>...])
  list(POP_FRONT <list> [<out-var>...])
  list(PREPEND <list> [<element>...])
  list(REMOVE_ITEM <list> <value>...)
  list(REMOVE_AT <list> <index>...)
  list(REMOVE_DUPLICATES <list>)
  list(TRANSFORM <list> <ACTION> [...])

Ordering
  list(REVERSE <list>)
  list(SORT <list> [...])

列表的读取

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}" )

输出:

>>> LENGTH: 4

 

 

GET: 用于读取列表中指定索引的元素,可以指定多个索引。

 list(GET <list> <element index> [<index> ...] <out-var>)

说明:1

  • <element index>为列表元素的索引,从0开始编号,索引0的元素为列表中的第一个元素;索引也可以是负数,-1表示列表的最后一个元素,-2表示列表倒数第二个元素,以此类推。注意:当索引(不管是正还是负)超过列表的长度,运行会报错(list index: XX out of range)。
  • <out-var> 为新创建的变量,存储指定索引元素的返回结果,也是一个列表。

1.例子:

 
# CMakeLists.txt
cmake_minimum_required (VERSION 3.12.2)
project (list_cmd_test)
set (list_test a b c d)
list (GET list_test 0 1 -1 -2 list_new)
message (     ">>> GET: ${list_new}" )

输出:

# 输出
>>> GET: a;b;d;c

 

 SUBLIST:子命令SUBLIST用于获取列表中的一部分(子列表)。

list (SUBLIST <list> <begin> <length> <output variable>)

返回列表<list>中,从索引<begin>开始,长度为<length>的子列表。如果长度<length>为0,返回的时空列表。如果长度<length>为-1或列表的长度小于<begin>+<length>,那么将列表中从<begin>索引开始的剩余元素返回。

# 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 (SUBLIST list_test 1 2 list_new)
message (">>> SUBLIST: ${list_new}")
list (SUBLIST list_test 1 0 list_new)
message (">>> SUBLIST: ${list_new}")
list (SUBLIST list_test 1 -1 list_new)
message (">>> SUBLIST: ${list_new}")
list (SUBLIST list_test 1 8 list_new)
message (">>> SUBLIST: ${list_new}")

输出:

>>> SUBLIST: b;c
>>> SUBLIST:
>>> SUBLIST: b;c;d
>>> SUBLIST: b;c;d

APPEND:子命令APPEND用于将元素追加到列表。

 list(APPEND <list> [<element>...])

1. 例子

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}")

输出:

>>> APPEND: a;b;c;d;1;2;3;4

 

INSERT:子命令INSERT用于在指定位置将元素(一个或多个)插入到列表中。

# CMakeLists.txt
cmake_minimum_required (VERSION 3.12.2)
project (list_cmd_test)
set (list_test a b c d)
list (INSERT list_test 0 8 8 8 8)
message (">>> INSERT: ${list_test}")
list (INSERT list_test -1 9 9 9 9)
message (">>> INSERT: ${list_test}")
list (LENGTH list_test lenght)
list (INSERT list_test ${length} 0)
message (">>> INSERT: ${list_test}")

输出:

>>> INSERT: 8;8;8;8;a;b;c;d
>>> INSERT: 8;8;8;8;a;b;c;9;9;9;9;d
>>> INSERT: 8;8;8;8;a;b;c;9;9;9;9;d;0

 

FIND:子命令FIND用于查找列表是否存在指定的元素。

list (FIND <list> <value> <output variable>)

 

如果列表<list>中存在<value>,那么返回<value>在列表中的索引,如果未找到则返回-1。

1. 例子:

cmake_minimum_required (VERSION 3.12.2)
project (list_cmd_test)
set (list_test 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}")

输出:

>>> FIND 'd': 3
>>> FIND 'e': -1

 

REVERSE:反转列表

list(REVERSE <list>)

1. 例子:

# CMakeLists.txt
cmake_minimum_required (VERSION 3.12.2)
project (list_cmd_test)
set (list_test aa bb cc dd) 
message (">>> list: ${list_test}")
list (REVERSE list_test)
message (">>> REVERSE: ${list_test}")

输出:

>>> list: aa;bb;cc;dd
>>> REVERSE: dd;cc;bb;aa

 

 

cmake_minimum_required (VERSION 3.12.2)
project (list_cmd_test)
set (list_test 3 1 1.1 10.1 3.4 9)
list (SORT list_test) # 以字母顺序,按照大小写敏感方式,升序排列
message (         ">>> SORT STRING-SENSITIVE-ASCENDING: ${list_test}" )
list (SORT list_test COMPARE NATURAL ORDER DESCENDING) # 以自然顺序,降序排列
message (         ">>> SORT STRING-SENSITIVE-DESCENDING: ${list_test}" )

输出:

>>> SORT STRING-SENSITIVE-ASCENDING: 1;1.1;10.1;3;3.4;9
>>> SORT STRING-SENSITIVE-DESCENDING: 10.1;9;3.4;3;1.1;1

 

参考资料

1. Cmake命令之list介绍

 

posted @ 2022-04-16 22:03  苏格拉底的落泪  阅读(121)  评论(0编辑  收藏  举报