CMakeLists.txt语法规则:条件判断中表达式说明四

一. 简介

前面学习了 CMakeLists.txt语法中的 部分常用命令,常量变量,双引号的使用。

前面几篇文章也简单了解了 CMakeLists.txt语法中的条件判断,文章如下:

CMakeLists.txt语法规则:条件判断说明一-CSDN博客

CMakeLists.txt语法规则:条件判断中表达式说明一-CSDN博客

CMakeLists.txt语法规则:条件判断中表达式说明二-CSDN博客
CMakeLists.txt语法规则:条件判断中表达式说明三-CSDN博客

本文继续学习 CMakeLists.txt语法中的条件判断,主要学习 条件判断中的 表达式。

二. CMakeLists.txt语法规则:条件判断中表达式

1. DEFINED <variable>表达式

如果给定的变量已经定义,则条件判断为真,否则为假;只要变量已经被设置(定义),if 条件判断就 是真,至于变量的值是真还是假并不重要。

复制代码
#输出为:false
if(DEFINED yyds)
message(true)
else()
message(false)
endif()

#输出为:true
set(yyds "YYDS")
if(DEFINED yyds)
message(true)
else()
message(false)
endif()

2. <variable|string> LESS <variable|string>表达式

如果左边给定的字符串或变量的值是有效数字并且小于右侧的值,则为真。否则为假。
测试如下:

复制代码
#输出为:false
if(100 LESS 20)
message(true)
else()
message(false)
endif()

#输出为:true
if(20 LESS 100)
message(true)
else()
message(false)
endif()

3. <variable|string> GREATER <variable|string>表达式

如果左边给定的字符串或变量的值是有效数字并且大于右侧的值,则为真。否则为假。
测试如下:

复制代码
#输出为:false
if(20 GREATER 100)
message(true)
else()
message(false)
endif()

#输出为:true
if(100 GREATER 20)
message(true)
else()
message(false)
endif()

4. <variable|string> EQUAL <variable|string>表达式

如果左边给定的字符串或变量的值是有效数字并且等于右侧的值,则为真。否则为假。

测试如下:

复制代码
#输出为:false
if(100 EQUAL 20)
message(true)
else()
message(false)
endif()

#输出为:true
if(100 EQUAL 100)
message(true)
else()
message(false)
endif()

三. elseif 分支

可以使用 elseif 组成多个不同的分支:

复制代码
set(MY_LIST Hello World China)

if(Hello IN_LIST MY_LIST)
message(Hello)
elseif(World IN_LIST MY_LIST)
message(World)
elseif(China IN_LIST MY_LIST)
message(China)
else()
message(false)
endif()

条件判断学习到这里。

相关推荐
平行云PVT4 小时前
数字孪生信创云渲染技术解析:从混合信创到全国产化架构
linux·unity·云原生·ue5·图形渲染·webgl·gpu算力
xdscode5 小时前
Linux云服务器安装openclaw,并对接飞书通道
linux·服务器·飞书·openclaw
Percep_gan5 小时前
Linux中安装Redis,很详细
linux·运维·redis
2401_877274246 小时前
从匿名管道到 Master-Slave 进程池:Linux 进程间通信深度实践
linux·服务器·c++
feng_you_ying_li6 小时前
linux之用户的权限详解(4)
linux·运维·服务器
二进制person7 小时前
JavaEE初阶 --网络编程
linux·服务器·网络
Cyber4K7 小时前
【妙招系列】Harbor 镜像私有仓库搭建手册
linux·云原生·容器
Irissgwe8 小时前
进程间通信
linux·服务器·网络·c++·进程间通信
创世宇图9 小时前
阿里云Alibaba Cloud Linux 4 LTS 64位生产环境配置-Nginx
linux·nginx
待续3019 小时前
OpenClaw 安装及使用教程(Windows / macOS / Linux)
linux·windows·macos