计算机
c base
c++ 基础 作用域及生命周期
c++ template
c++ 内存视角
c++ 函数指针
c++ 基础 基础语法
c++ 多线程
c++ 性能
c++ 类 基础
c++ 类 对象模型 类析构
c++ 类 设计模式
cmake
CMAKE环境搭建 windows
创建第一个cmakelists.txt
构建稍复杂的项目
动态链接库
EX1
EX1 START
EX1 ANSWER
EX2
EX2 START
EX2 ANSWER
EX3
EX3 START
EX3 ANSWER
变量
控制流程
函数和宏
查找和使用外部库
生成器表达式
qt 开发环境
qt c++
理解QObject 1
理解QObject 4
qt index
qt qml quick
qt ui
qt 多线程
数据结构 数组
PC问题监控及排查
PC程序性能优化
OS
TOOL
C++ STL
编程漫谈
C++实战-生产者 消费者流水线
C++实战 IO
本站点使用 MrDoc 构建
-
+
控制流程
## 基本语法 ##### 条件语句(if/else) ```cmake if(condition) # 条件为真时执行 endif() if(condition) # 条件为真时执行 else() # 条件为假时执行 endif() if(condition1) # condition1 为真 elseif(condition2) # condition2 为真 else() # 都为假 endif() ``` ##### 检查变量 ```cmake # 检查变量是否定义 if(DEFINED MY_VAR) message("MY_VAR is defined: ${MY_VAR}") endif() # 检查变量是否为空 set(EMPTY_VAR "") if(NOT EMPTY_VAR) message("EMPTY_VAR is empty or undefined") endif() ``` ##### 字符串比较 ```cmake set(BUILD_TYPE "Debug") # ❌ 错误:使用 EQUAL 进行字符串比较 if(BUILD_TYPE EQUAL "Debug") # 会尝试数值比较 message("Wrong!") endif() # ✅ 正确:使用 STREQUAL if(BUILD_TYPE STREQUAL "Debug") message("Correct!") endif() ``` ##### 数值比较 ```cmake set(VERSION 15) # 数值比较运算符 if(VERSION EQUAL 15) message("Version is 15") endif() if(VERSION GREATER 10) message("Version is greater than 10") endif() if(VERSION LESS 20) message("Version is less than 20") endif() if(VERSION GREATER_EQUAL 15) message("Version is 15 or greater") endif() if(VERSION LESS_EQUAL 15) message("Version is 15 or less") endif() ``` ##### 逻辑运算 ```cmake set(ENABLE_A ON) set(ENABLE_B OFF) # AND 运算 if(ENABLE_A AND ENABLE_B) message("Both enabled") endif() # OR 运算 if(ENABLE_A OR ENABLE_B) message("At least one enabled") endif() # 复杂条件 if((ENABLE_A OR ENABLE_B) AND NOT ENABLE_C) message("Complex condition met") endif() ``` ##### 文件和目录检查 ```cmake # 检查文件是否存在 if(EXISTS "${CMAKE_SOURCE_DIR}/config.txt") message("Config file found") endif() # 检查是否为目录 if(IS_DIRECTORY "${CMAKE_SOURCE_DIR}/src") message("src directory exists") endif() # 检查是否为符号链接 if(IS_SYMLINK "${CMAKE_SOURCE_DIR}/link") message("It's a symbolic link") endif() # 检查文件是否比另一个文件新 if("${CMAKE_SOURCE_DIR}/file1.txt" IS_NEWER_THAN "${CMAKE_SOURCE_DIR}/file2.txt") message("file1.txt is newer") endif() ``` ##### 平台检查 ```cmake # 检查操作系统 if(WIN32) message("Building on Windows") elseif(UNIX) if(APPLE) message("Building on macOS") else() message("Building on Linux/Unix") endif() endif() # 检查编译器 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") message("Using GCC") elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") message("Using Clang") elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") message("Using MSVC") endif() ``` ## 循环 ##### for...each... ```cmake # 从 0 到 4 foreach(i RANGE 4) message("Number: ${i}") endforeach() # 输出: 0, 1, 2, 3, 4 # 从 1 到 5 foreach(i RANGE 1 5) message("Number: ${i}") endforeach() # 输出: 1, 2, 3, 4, 5 # 从 0 到 10,步长为 2 foreach(i RANGE 0 10 2) message("Number: ${i}") endforeach() # 输出: 0, 2, 4, 6, 8, 10 ``` ###### 遍历多个列表(ZIP) ```cmake set(NAMES Alice Bob Charlie) set(AGES 25 30 35) foreach(name age IN ZIP_LISTS NAMES AGES) message("${name} is ${age} years old") endforeach() # 输出: # Alice is 25 years old # Bob is 30 years old # Charlie is 35 years old ``` ```cmake # 遍历目录中的文件 file(GLOB SOURCE_FILES "src/*.cpp") foreach(source_file ${SOURCE_FILES}) message("Found source: ${source_file}") endforeach() ``` ##### while ```cmake set(counter 0) while(counter LESS 5) message("Counter: ${counter}") math(EXPR counter "${counter} + 1") endwhile() # 输出: 0, 1, 2, 3, 4 ``` ###### 实用示例:查找文件 ```cmake set(dir "${CMAKE_SOURCE_DIR}") set(found FALSE) while(NOT found AND NOT dir STREQUAL "/") if(EXISTS "${dir}/config.cmake") set(found TRUE) message("Found config at: ${dir}") else() get_filename_component(dir "${dir}" DIRECTORY) endif() endwhile() ``` ##### break() continue()
peipeo
2026年5月16日 10:51
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码