CMake语法—流程控制(if-foreach-while)
2021/12/1 23:36:29
本文主要是介绍CMake语法—流程控制(if-foreach-while),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录- CMake语法—流程控制(if-foreach-while)
- 1.流程控制
- 1.1 代码结构
- 1.2 示例代码
- 1.3 运行结果
- 1.4 小结
- 1.流程控制
CMake语法—流程控制(if-foreach-while)
1.流程控制
1.1 代码结构
- learn_cmake:为根目录
- build:为CMake配置输出目录(在此例中即生成sln解决方案的地方)
- cmake_config.bat:执行CMake配置过程的脚本(双击直接运行)
- CMakeLists.txt:CMake脚本
1.2 示例代码
-
示例代码(CMakeLists.txt文件内容)
cmake_minimum_required(VERSION 3.18) # 设置工程名称 set(PROJECT_NAME KAIZEN) # 设置工程版本号 set(PROJECT_VERSION "1.0.0.10" CACHE STRING "默认版本号") # 工程定义 project(${PROJECT_NAME} LANGUAGES CXX C VERSION ${PROJECT_VERSION} ) # 打印开始日志 message(STATUS "\n########## BEGIN_TEST_PROCESS_CONTROL") # if example set(var_3 "camke") if (DEFINED var_1) message("defined var_1") elseif (DEFINED var_2) message("defined var_2") elseif (DEFINED var_3) message("defined var_3") else() message("defined nothing") endif() # endif ## foreach example set(var_list "C++" "JAVA" Python "CMake" 1024) foreach(item ${var_list}) message("当前变量是:${item}") endforeach() set(result 0) set(end 0) foreach(element RANGE 0 100) math(EXPR result "${result} + ${element}") if (result LESS 100) continue() ## continue endif() if (result GREATER 1000) set(end ${element}) break() ## break endif() endforeach() message("from 0 plus to ${end} is : ${result}") ## endforeach ### while example set(var_max 5) while(${var_max} GREATER 0) message(${var_max}) math(EXPR var_max "${var_max} - 1") endwhile() ### endwhile # 打印结束日志 message(STATUS "########## END_TEST_PROCESS_CONTROL\n")
-
cmake_config.bat
@echo off set currentDir=%~dp0 set buildDir=%currentDir% set cmakeOutputDir=%currentDir%\build cmake -S %buildDir% -B %cmakeOutputDir% -G"Visual Studio 16 2019" -T v140 -A x64 pause
1.3 运行结果
-
本地环境
本地安装VS版本:Visual Studio 2019(2015工具集)
CMake版本:3.18.2
F:\learn_cmake λ cmake --version cmake version 3.18.2 CMake suite maintained and supported by Kitware (kitware.com/cmake).
-
输出结果
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.17763. -- The CXX compiler identification is MSVC 19.0.24245.0 -- The C compiler identification is MSVC 19.0.24245.0 -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe - skipped -- Detecting C compile features -- Detecting C compile features - done -- ########## BEGIN_TEST_PROCESS_CONTROL defined var_3 当前变量是:C++ 当前变量是:JAVA 当前变量是:Python 当前变量是:CMake 当前变量是:1024 from 0 plus to 45 is : 1035 5 4 3 2 1 -- ########## END_TEST_PROCESS_CONTROL -- Configuring done -- Generating done -- Build files have been written to: F:/learn_cmake/build 请按任意键继续. . .
1.4 小结
-
if 概要
if(<condition>) <commands> elseif(<condition>) # optional block, can be repeated <commands> else() # optional block <commands> endif()
具体可参考最钟情官方文档:https://cmake.org/cmake/help/latest/command/if.html
-
foreach 概要
foreach(<loop_var> <items>) <commands> endforeach()
具体可参考最钟情官方文档:https://cmake.org/cmake/help/latest/command/foreach.html
-
while 概要
while(<condition>) <commands> endwhile()
具体看参考最钟情官方文档:https://cmake.org/cmake/help/latest/command/while.html
这篇关于CMake语法—流程控制(if-foreach-while)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-25TypeScript基础知识详解
- 2024-12-25安卓NDK 是什么?-icode9专业技术文章分享
- 2024-12-25caddy 可以定义日志到 文件吗?-icode9专业技术文章分享
- 2024-12-25wordfence如何设置密码规则?-icode9专业技术文章分享
- 2024-12-25有哪些方法可以实现 DLL 文件路径的管理?-icode9专业技术文章分享
- 2024-12-25错误信息 "At least one element in the source array could not be cast down to the destination array-icode9专业技术文章分享
- 2024-12-25'flutter' 不是内部或外部命令,也不是可运行的程序 或批处理文件。错误信息提示什么意思?-icode9专业技术文章分享
- 2024-12-25flutter项目 as提示Cannot resolve symbol 'embedding'提示什么意思?-icode9专业技术文章分享
- 2024-12-24怎么切换 Git 项目的远程仓库地址?-icode9专业技术文章分享
- 2024-12-24怎么更改 Git 远程仓库的名称?-icode9专业技术文章分享