控制语句
if else
if(<condition>)
...
endif()
比如:
if (NOT IS_DIRECTORY "/usr/local/lib")
message(FATAL_ERROR "lib folder not found")
endif()
if(CMAKE_BUILD_TYPE MATCHES Debug)
message("Is in debug mode")
endif()
if (WIN32)
message("In windows env")
elif (LINUX)
message("In linux env")
endif()
cmake_policy(SET CMP0057 NEW)
set(SRC_FILES list.h list.c math.h math.c)
if ("math.h" IN_LIST SRC_FILES)
message("math.h already in src list")
endif()
这里的条件判断方式比较多, 可以参考 CMake if 手册
foreach && while
遍历一个列表:
foreach(FILE ${SRC_FILES})
message("SRC FILE: ${FILE}")
endforeach()
以下示例打印出 1
至 10
之间所有的奇数, 类似于 python
中的 range()
函数:
foreach(ODD RANGE 1 10 2)
message("Odd num: ${ODD}")
endforeach()
break && continue
在循环中使用.
return
用于从函数, 目录或者文件中返回, 注意它不支持返回数据.