如下例所示,CMake 中的函数如何提前返回?
function do_the_thing(HAS_PROPERTY_A)
# don't do things that have property A when property A is disabled globally
if (PROPERTY_A_DISABLED AND HAS_PROPERTY_A)
# What do you put here to return?
endif()
# do things and implement magic
endfunction()
最佳答案
您使用 return()
(CMake 手册页 here ),如果在函数中调用,则从函数返回。
例如:
cmake_minimum_required(VERSION 3.0)
project(returntest)
# note: your function syntax was wrong - the function name goes
# after the parenthesis
function (do_the_thing HAS_PROPERTY_A)
if (HAS_PROPERTY_A)
message(STATUS "Early Return")
return()
endif()
message(STATUS "Later Return")
endfunction()
do_the_thing(TRUE)
do_the_thing(FALSE)
结果是:
$ cmake ../returntest
-- Early Return
-- Later Return
-- Configuring done
...
它也适用于函数之外:如果你从
include()
调用它如果你通过 add_subdirectory()
从文件中调用 if ,它会返回到包含器中。 ,它会将您返回到父文件。
关于cmake - 如何提前从 cmake 中的函数返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41156888/