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 ## foreach 方式一: set(var_list "C++" "JAVA" Python "CMake" 1024) foreach(item ${var_list}) message("当前变量是:${item}") endforeach() ## foreach 方式二: 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}\n") ## foreach 方式三: foreach(every RANGE 0 10 2) message(STATUS "every = ${every}") endforeach() ## foreach 方式四: set(X a b c) foreach(var IN LISTS X) message(STATUS "var = ${var}") endforeach() ## foreach 方式五: set(A a b c) set(B 3 4 5) set(C "11 22") set(D 8;9 10) set(E "") foreach(m IN LISTS A B C D E) message(STATUS "m = ${m}") endforeach() ## foreach 方式六: foreach(pair IN ZIP_LISTS A B) message(STATUS "pair_0 = ${pair_0}, pair_1 = ${pair_1}") endforeach() ## foreach 方式七: foreach(first second IN ZIP_LISTS A B) message(STATUS "first = ${first}, second = ${second}") endforeach() ## endforeach ### while 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 -- every = 0 -- every = 2 -- every = 4 -- every = 6 -- every = 8 -- every = 10 -- var = a -- var = b -- var = c -- m = a -- m = b -- m = c -- m = 3 -- m = 4 -- m = 5 -- m = 11 22 -- m = 8 -- m = 9 -- m = 10 -- pair_0 = a, pair_1 = 3 -- pair_0 = b, pair_1 = 4 -- pair_0 = c, pair_1 = 5 -- first = a, second = 3 -- first = b, second = 4 -- first = c, second = 5 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(
) elseif( ) # optional block, can be repeated else() # optional block endif() 具体可参考最钟情官方文档:https://cmake.org/cmake/help/latest/command/if.html
-
foreach 概要
foreach(
) endforeach() 具体可参考最钟情官方文档:https://cmake.org/cmake/help/latest/command/foreach.html
-
while 概要
while(
) endwhile() 具体看参考最钟情官方文档:https://cmake.org/cmake/help/latest/command/while.html