1. 程式人生 > 實用技巧 >cmake命令用法整理list命令

cmake命令用法整理list命令

用途:提供一些列表操作

list(LENGTH <list><output variable>)
list(GET <list> <elementindex> [<element index> ...]
<output variable>)
list(APPEND <list><element> [<element> ...])
list(FIND <list> <value><output variable>)
list(INSERT <list><element_index> <element> [<element> ...])
list(REMOVE_ITEM <list> <value>[<value> ...])
list(REMOVE_AT <list><index> [<index> ...])
list(REMOVE_DUPLICATES <list>)
list(REVERSE <list>)
list(SORT <list>)

LENGTH返回列表的長度

GET返回列表中指定下標的元素

APPEND新增新元素到列表中

INSERT 將新元素插入到列表中指定的位置

REMOVE_ITEM從列表中刪除某個元素

REMOVE_AT從列表中刪除指定下標的元素

REMOVE_DUPLICATES從列表中刪除重複的元素

REVERSE 將列表的內容實地反轉,改變的是列表本身,而不是其副本

SORT 將列表按字母順序實地排序,改變的是列表本身,而不是其副本

列表的子命令APPEND, INSERT, REMOVE_AT, REMOVE_ITEM,REMOVE_DUPLICATES, REVERSE以及SORT在當前的CMake變數域建立一些新值。與SET命令類似,即使列表本身是在父域中定義的,LIST命令也只會在當前域建立新的變數值,為了將這些操作的結果向上傳遞,需要通過SET PARENT_SCOPE, SET CACHE INTERNAL或其他值域擴充套件的方法。

注意:cmake中的列表是以分號隔開的一組字串。可以使用set命令建立一個列表。例如:set(var a b c d e)建立了一個這樣的列表:a;b;c;d;e。 set(var “a b c d e”)建立了一個字串或只有一個元素的列表。

當指定索引值時,<element index>為大於或等於0的值。它從列表的開始處索引,0代表列表的第一個元素。如果<element index>為小於或等於-1的值,它從列表的結尾處索引,-1代表列表的最後一個元素。

There are two possible solutions:

Usefile (GLOB ...instead ofaux_source_directory

with a globbing expression that does not match that one file but includes all the others, e.g.:

file(GLOB _srcFiles "src/f[1-3].cpp")

This will match match filesf1.cpp,f2.cpp,f3.cpp, but notf4.cpp.

Or useaux_source_directoryand then remove the file to be excluded explicitly with alist(REMOVE_ITEMcommand, e.g.:

  1. aux_source_directory(src _srcFiles)
  2. list(REMOVE_ITEM _srcFiles "src/f4.cpp")

備註: 在 list remove 的時候,字串一定要保持一致,使用 MESSAGE(STATUS xxx) 打印出來,是個不錯的選擇。