如何从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()
答案 0 :(得分:5)
您使用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()
ed文件调用它,它会返回到includer,如果你通过add_subdirectory()
从文件中调用它,它会返回到父文件。