示例代码(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
本地环境
本地安装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 请按任意键继续. . .
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