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-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享