做網(wǎng)站 警察佛山抖音seo
1.雙引號
1.1 命令參數(shù)
1)介紹
????????命令中多個參數(shù)之間使用空格進行分隔,而 cmake 會將雙引號引起來的內(nèi)容作為一個整體,當它當成一個參數(shù),假如你的參數(shù)中有空格(空格是參數(shù)的一部分),那么就可以使用雙引號。
message(Hello World)
message("Hello World")
????????第一個 message 命令傳入了兩個參數(shù),而第二個 message 命令只傳入一個參數(shù);
????????在第一個 message 命令中,打印信息時,會將兩個獨立的字符串 Hello 和 World 都打印出來,而且World 會緊跟在Hello 之后。而第二個 message 命令只有一個參數(shù)。
HelloWorld
Hello World
1.2引用變量
1)介紹
? ? ? ? 加上“ ”可以讓cmake把這個數(shù)組的所有元素當成一個整體,而不是分散的個體。
2)例子
?(1)? ? ? ?
????????不加雙引號
# CMakeLists.txt
set(MY_LIST Hello World China)
message(${MY_LIST})
? ? ? ? 執(zhí)行結果
HelloWorldChina
?(2)
? ? ? ? 加雙引號
# CMakeLists.txt
set(MY_LIST Hello World China)
message("${MY_LIST}")
? ? ? ? 執(zhí)行結果
Hello;World;China
?2.條件判斷
2.1格式
if(expression)# then section.command1(args ...)command2(args ...)...
elseif(expression2)# elseif section.command1(args ...)command2(args ...)...
else(expression)# else section.command1(args ...)command2(args ...)...
endif(expression)
2.2 expression表達式
?
2.3表達式詳解
?2.3.1 <constant>
????????在 cmake 中,可以把 1、ON、YES、TRUE、Y 或非零數(shù)字以及 0、OFF、NO、FALSE、N、IGNORE、NOTFOUND、空字符串或以后綴-NOTFOUND 結尾這些理解為常量,類似于布爾值,而且它們不區(qū)分大小 寫;如果參數(shù)不是這些特定常量之一,則將其視為變量或字符串,并使用除<constant>之外的表達式。
2.3.2?<variable/string>
????????在 if(<variable/string>)條件判斷中,如果變量已經(jīng)定義,并且它的值是一個非假常量,則條件為真;否則為假,注意宏參數(shù)不是變量。
? ? ? ? 真:變量定義+非假常量
? ? ? ? 假:未定義/假常量
2.3.3?NOT <expression>
????????NOT 其實就類似于 C 語言中的取反,在 if(NOT <expression>)條件判斷中,如果表達式 expression 為真,則條件判斷為假;如果表達式 expression 為假,則條件判斷為真。
2.3.4?<expr1> AND <expr2>
????????類似于 C 語言中的邏輯與(&&),只有 expr1 和 expr2 同時為真時,條件判斷才為真;否則條件判斷為假。
2.3.5?<expr1> OR <expr2>
????????類似于 C 語言中的邏輯或(||),當 expr1 或 expr2 至少有一個為真時,條件判斷為真;否則為假。
2.3.6 COMMAND command-name
1)介紹? ? ? ??
????????如果 command-name 是一個已經(jīng)定義的命令、宏或函數(shù)時,條件判斷為真;否則為假。
?2)例子
if(COMMAND yyds)message(true)
else()message(false)
endif()
輸出:false
if(COMMAND project)message(true)
else()message(false)
endif()
輸出:true
2.3.7??TARGET target-name
if(TARGET hello)message(true)
else()message(false)
endif()
? ? ? ? 輸出為false
add_library(hello hello.c)
if(TARGET hello)message(true)
else()message(false)
endif()
? ? ? ? 輸出為true
2.3.8?EXISTS path
set(MY_STR "Hello World")
if(MY_STR MATCHES "Hello World")message(true)
else()message(false)
endif()
? ? ? ? 輸出為true
set(MY_STR "Hello World")
if("Hello World" MATCHES "Hello World")message(true)
else()message(false)
endif()
? ? ? ? 輸出為true
2.3.9 <variable|string> IN_LIST <variable>
2.3.10 DEFINED <variable>
2.3.11 <variable|string> LESS <variable|string>
2.3.12 <variable|string> GREATER <variable|string>
????????如果左邊給定的字符串或變量的值是有效數(shù)字并且大于右側的值,則為真。否則為假。
2.3.13<variable|string> EQUAL <variable|string>
3.循環(huán)語句
3.1foreach
1)基本語法
foreach(loop_var arg1 arg2 ...)command1(args ...)command2(args ...)...
endforeach(loop_var)
# foreach 循環(huán)測試
set(my_list hello world china)
foreach(loop_var ${my_list})message("${loop_var}")
endforeach()
? ? ? ? 打印輸出信息如下
2)RANGE 關鍵字
foreach(loop_var RANGE stop)
foreach(loop_var RANGE start stop [step])
foreach(loop_var IN [LISTS [list1 [...]]][ITEMS [item1 [...]]])
3.2while
while(condition)command1(args ...)command1(args ...)...
endwhile(condition)
3.3break/continue
4.math
? ? ? ? 用到之后再學習