Previous exciting content:CMake Hello, WorldCMake VariablesCMake Official Tutorial (Basic Project Setup)CMake Official Tutorial (Library Creation)CMake Official Tutorial (Usage Requirements)CMake Official Tutorial (Installation and Testing)CMake Common Syntax (if Statement)CMake Common Syntax (Cache Variables)CMake Common Syntax (Environment Variables)CMake Common Syntax (Mathematical Calculations)CMake Common Syntax (Strings)CMake Common Syntax (Lists)
CMake primarily has two types of loops: foreach and while loops, with the basic forms as follows:
foreach(<loop_var> <items>)
<commands>
endforeach()
#
while(<condition>)
<commands>
endwhile()
For the foreach loop, the following specific forms are available:
# Range iteration: single parameter form, iterates all non-negative integers from 0 to <stop> (inclusive)
foreach(<loop_var> RANGE <stop>)
# Range iteration: specify start value, end value, and step (default step=1)
foreach(<loop_var> RANGE <start> <stop> [<step>])
# List and item iteration
foreach(<loop_var> IN [LISTS [<lists>]] [ITEMS [<items>]])
# Parallel iteration over multiple lists
foreach(<loop_var>... IN ZIP_LISTS <lists>)
Examples of range iteration and list/item iteration are as follows:
#[0,6]
foreach(var RANGE 6)
message("${var}")
endforeach()
#1,3,5,7,9
foreach(var RANGE 192)
message("${var}")
endforeach()
foreach(elem IN ITEMS a.c b.c c.c d.c)
message("${elem}")
endforeach()
set(src a.cpp b.cpp c.cpp d.cpp)
foreach(elem IN LISTS src)
message("${elem}")
endforeach()
set(header a.hpp b.hpp c.hpp d.hpp)
foreach(elem IN LISTS src header)
message("${elem}")
endforeach()
CMake 3.17 supports parallel iteration over multiple lists, with the iteration rules as follows:
- • If only one loop variable is specified, it generates
<span>variable_name_0</span>
,<span>variable_name_1</span>
, etc., corresponding to the current elements of each list. - • If multiple loop variables are specified, the number must match the number of lists; if a list is shorter, the corresponding variable will be undefined during iteration.
The official example is as follows:
list(APPEND English one two three four)
list(APPEND Bahasa satu dua tiga)
foreach(num IN ZIP_LISTS English Bahasa)
message(STATUS "num_0=${num_0}, num_1=${num_1}")
endforeach()
foreach(en ba IN ZIP_LISTS English Bahasa)
message(STATUS "en=${en}, ba=${ba}")
endforeach()
The output results are:
-- num_0=one, num_1=satu
-- num_0=two, num_1=dua
-- num_0=three, num_1=tiga
-- num_0=four, num_1=
-- en=one, ba=satu
-- en=two, ba=dua
-- en=three, ba=tiga
-- en=four, ba=
For the while command, the syntax of <span><condition></span>
is completely consistent with the conditional judgment rules of the <span>if()</span>
command; refer to previous articles on CMake common syntax (if statement).
A simple (basic decrement loop) example is as follows:
set(counter 3)
while(counter GREATER 0)
message("Current counter value: ${counter}")
# Decrement counter
math(EXPR counter "${counter} - 1")
# Exit early when counter is 0
if(counter EQUAL 0)
message("Counter reached zero, exiting loop")
break()
endif()
endwhile()
Commands related to loops also include <span>break()</span>
and <span>continue()</span>
, where the former terminates the loop early and the latter skips the current iteration, which is similar to general programming languages and will not be repeated here.