做網(wǎng)站 警察佛山抖音seo
1.雙引號
1.1 命令參數(shù)
1)介紹
????????命令中多個(gè)參數(shù)之間使用空格進(jìn)行分隔,而 cmake 會將雙引號引起來的內(nèi)容作為一個(gè)整體,當(dāng)它當(dāng)成一個(gè)參數(shù),假如你的參數(shù)中有空格(空格是參數(shù)的一部分),那么就可以使用雙引號。
message(Hello World)
message("Hello World")
????????第一個(gè) message 命令傳入了兩個(gè)參數(shù),而第二個(gè) message 命令只傳入一個(gè)參數(shù);
????????在第一個(gè) message 命令中,打印信息時(shí),會將兩個(gè)獨(dú)立的字符串 Hello 和 World 都打印出來,而且World 會緊跟在Hello 之后。而第二個(gè) message 命令只有一個(gè)參數(shù)。
HelloWorld
Hello World
1.2引用變量
1)介紹
? ? ? ? 加上“ ”可以讓cmake把這個(gè)數(shù)組的所有元素當(dāng)成一個(gè)整體,而不是分散的個(gè)體。
2)例子
?(1)? ? ? ?
????????不加雙引號
# CMakeLists.txt
set(MY_LIST Hello World China)
message(${MY_LIST})
? ? ? ? 執(zhí)行結(jié)果
HelloWorldChina
?(2)
? ? ? ? 加雙引號
# CMakeLists.txt
set(MY_LIST Hello World China)
message("${MY_LIST}")
? ? ? ? 執(zhí)行結(jié)果
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表達(dá)式
?
2.3表達(dá)式詳解
?2.3.1 <constant>
????????在 cmake 中,可以把 1、ON、YES、TRUE、Y 或非零數(shù)字以及 0、OFF、NO、FALSE、N、IGNORE、NOTFOUND、空字符串或以后綴-NOTFOUND 結(jié)尾這些理解為常量,類似于布爾值,而且它們不區(qū)分大小 寫;如果參數(shù)不是這些特定常量之一,則將其視為變量或字符串,并使用除<constant>之外的表達(dá)式。
2.3.2?<variable/string>
????????在 if(<variable/string>)條件判斷中,如果變量已經(jīng)定義,并且它的值是一個(gè)非假常量,則條件為真;否則為假,注意宏參數(shù)不是變量。
? ? ? ? 真:變量定義+非假常量
? ? ? ? 假:未定義/假常量
2.3.3?NOT <expression>
????????NOT 其實(shí)就類似于 C 語言中的取反,在 if(NOT <expression>)條件判斷中,如果表達(dá)式 expression 為真,則條件判斷為假;如果表達(dá)式 expression 為假,則條件判斷為真。
2.3.4?<expr1> AND <expr2>
????????類似于 C 語言中的邏輯與(&&),只有 expr1 和 expr2 同時(shí)為真時(shí),條件判斷才為真;否則條件判斷為假。
2.3.5?<expr1> OR <expr2>
????????類似于 C 語言中的邏輯或(||),當(dāng) expr1 或 expr2 至少有一個(gè)為真時(shí),條件判斷為真;否則為假。
2.3.6 COMMAND command-name
1)介紹? ? ? ??
????????如果 command-name 是一個(gè)已經(jīng)定義的命令、宏或函數(shù)時(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ù)字并且大于右側(cè)的值,則為真。否則為假。
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 關(guān)鍵字
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
? ? ? ? 用到之后再學(xué)習(xí)