【CMake 语法】(4) CMake 命令、命令参数、转义序列

1. 命令

CMake 源码文件基本上由命令组成。

2. 命令参数

命令调用中由三种类型参数:

  • 括号参数
  • 带引号的参数
  • 不带引号的参数

2.1 括号参数

括号参数参数内容,是左括号 [ 跟零个或多个 = 以右括号 ] 结束。

括号参数,不执行 转义序列变量引用

例如:

message([=[
This is the first line in a bracket argument with bracket length 1.
No \-escape sequences or ${variable} references are evaluated.
This is always one argument even though it contains a ; character.
The text does not end on a closing bracket of length 0 like ]].
It does end in a closing bracket of length 1.
]=])

注意: 3.0 之前的 CMake 版本不支持括号参数

2.2 带引号的参数

带引号的参数参数内容,在双引号 " 之间 。

例如:

message("This is a quoted argument containing multiple lines.
This is always one argument even though it contains a ; character.
Both \\-escape sequences and ${variable} references are evaluated.
The text does not end on an escaped double-quote like \".
It does end in an unescaped double quote.
")

使用反斜杠 \,连接行。

message("\
This is the first line of a quoted argument. \
In fact it is the only line but since it is long \
the source code uses line continuation.\
")

注意: 3.0 之前的 CMake 版本不支持继续使用 \.

2.3 不带引号的参数

参数用 空格 、分号 ;、换行 \n 分隔不同的参数,如果使用转义字符 \,并不分隔参数

例如:

foreach(arg
    NoSpace
    Escaped\ Space
    This;Divides;Into
    Five Arguments
    Escaped\;Semicolon
    )
  message("${arg}")
endforeach()

3. 转义序列

我们通过 \ 之后跟一个字符,组成转义序列

  • 转义编码,包括 '\t''\r''\n'
  • 转义符号,包括 '\;''\\'

转移序列作为参数的值。

案例: 说明 命令、命令参数、转义序列

cmake_minimum_required(VERSION 3.0.0)

# 1. Bracket Argument
message("======> Bracket Argument")
message([=[
This is the first line in a bracket argument with bracket length 1.
No \-escape sequences or ${variable} references are evaluated.
This is always one argument even though it contains a ; character.
The text does not end on a closing bracket of length 0 like ]].
It does end in a closing bracket of length 1.
]=])


# 2. Quoted Argument
message("======> Quoted Argument")
message("This is a quoted argument containing multiple lines.
This is always one argument even though it contains a ; character.
Both \\-escape sequences and ${variable} references are evaluated.
The text does not end on an escaped double-quote like \".
It does end in an unescaped double quote.
")


# 3. Connecting line
message("======> Connecting line")
message("\
This is the first line of a quoted argument. \
In fact it is the only line but since it is long \
the source code uses line continuation.\
")


# 4. Unquoted Argument
message("======> Unquoted Argument")
foreach(arg
    NoSpace
    Escaped\ Space
    This;Divides;Into
    Five Arguments
    Escaped\;Semicolon
    )
  message("${arg}")
endforeach()

# 5. Escape Sequences
message("======> Escape Sequences")
message("
This is the first.\r\n\tline of a quoted argument.
")
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值