[CMake entry and advanced (10)] CMakeLists.txt grammar rules_loop statement (with code)

        In addition to if condition judgment, cmake also supports loop statements, including foreach() loop and while() loop.

foreach loop

        1. Basic usage of foreach

        The basic usage of the foreach loop is as follows:

foreach(loop_var arg1 arg2 ...)
     command1(args ...)
     command2(args ...)
     ...
endforeach(loop_var)

        The writing in the brackets of endforeach may not be written, if it is written, it must be consistent with that in foreach.

        The parameter loop_var is a loop variable, and the variables in the parameter list will be assigned to him in turn during the loop, similar to the variable i often used in the for loop of C language.

# foreach 循环测试
foreach(loop_var A B C D)
     message("${loop_var}")
endforeach()

        The print information is:

A
B
C
D

        All elements in a list can be compiled using foreach as follows:

# foreach 循环测试
set(my_list hello world china)
foreach(loop_var ${my_list})
     message("${loop_var}")
endforeach()

        The print information is as follows:

        2. The RANGE keyword of the foreach loop

        Usage is as follows:

foreach(loop_var RANGE stop)
foreach(loop_var RANGE start stop [step])

        For the first method, the cycle will be from 0 to the specified number stop, including stop, and stop cannot be a negative number.

        For the second type, the cycle starts from the specified number start and ends at stop, and the step size is step, but the step parameter is an optional parameter. If not specified, the default step=1; the three parameters cannot be negative, and stop Cannot be smaller than start.

        Next we test, test one:

# foreach 循环测试
foreach(loop_var RANGE 4)
     message("${loop_var}")
endforeach()

        The print information is as follows:

         Test two:

# foreach 循环测试
foreach(loop_var RANGE 1 4 1)
    message("${loop_var}")
endforeach()

        The print information is as follows:

         3. The IN keyword of the foreach loop

        The usage is as follows:

foreach(loop_var IN [LISTS [list1 [...]]]
             [ITEMS [item1 [...]]])

        Loop over each element in the list, or specify the element directly.

        Next, test, test one:

# foreach 循环测试
set(my_list A B C D)

foreach(loop_var IN LISTS my_list)
    message("${loop_var}")
endforeach()

        The print information is as follows:

         Test two:

# foreach 循环测试
foreach(loop_var IN ITEMS A B C D)
    message("${loop_var}")
endforeach()

        The print information is the same as above.

while loop

        The while loop is used as follows:

while(condition)
 command1(args ...)
 command1(args ...)
 ...
endwhile(condition)

        The condition in the brackets of endwhile can be written or not. If it is written, it must be consistent with the condition in while.

        The meaning of the while loop in cmake is the same as that of the while loop in C language, but when the condition condition is true, the commands in the loop body are executed, and the grammatical form of the conditional condition is the same as that in the if conditional judgment.

# while 循环测试
set(loop_var 4)
while(loop_var GREATER 0)
    message("${loop_var}")
    math(EXPR loop_var "${loop_var} - 1")
endwhile()

        The output is as follows:

         In the above example, the condition of the while loop is (loop_var greater 0), which is equivalent to (loop_var > 0). When the effective value of the loop_var variable is greater than 0, the while loop body is executed; the math in cmake is used in the while loop body Operation command math().

        In the body of the while loop, loop_var is printed, and then loop_var is decremented by one.

break、continue

        In cmake, you can also use break and continue statements similar to those in C language in the loop body.

        1、break

        The break() command is used to break out of the loop, and it has the same effect as in C language. The test is as follows:

# while...break 测试
set(loop_var 10)

while(loop_var GREATER 0) #loop_var>0 时 执行循环体
    message("${loop_var}")
    if(loop_var LESS 6) #当 loop_var 小于 6 时
         message("break")
         break() #跳出循环
    endif()
    math(EXPR loop_var "${loop_var} - 1")#loop_var--
endwhile()

        The print information is as follows:

         2、continue

        The continue() command is used to end this cycle and execute the next cycle. The test is as follows:

# while...continue 测试
# 打印所有偶数
set(loop_var 10)

while(loop_var GREATER 0) #loop_var>0 时 执行循环体
     math(EXPR var "${loop_var} % 2") #求余

     if(var EQUAL 0) #如果 var=0,表示它是偶数
         message("${loop_var}") #打印这个偶数
         math(EXPR loop_var "${loop_var} - 1")#loop_var--
         continue() # 执行下一次循环
     endif()

     math(EXPR loop_var "${loop_var} - 1")#loop_var--
endwhile()

        This cmake code is to find an even number between 0 and 10 (left closed and right open), and print out the even number. The continue() command is used, the code will not be explained, and the comment has been written very clearly. The printed results are as follows:

         The use of the break() and continue() commands is introduced here.

Guess you like

Origin blog.csdn.net/cj_lsk/article/details/131241078