cmake - 如何提前从 cmake 中的函数返回?

标签 cmake

如下例所示,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/

相关文章:

cmake - 如何在 CMake 中使用 cpplint 代码样式检查?

c++ - CMAKE/代码组织

c++ - 如何包含 <emscripten/emscripten.h>

windows - 需要在 github 操作工作流程中将反斜杠转换为正斜杠

cmake 找不到所需的包 TIFF

c++ - 带有子库和包含目录的库,我如何在 CMake 中做到这一点?

c++ - 在 OSX 10.11(CLion 1.1 中的 CMake 3.3)上使用 SDL2 和 SDL2_image

cmake - 如何使用CMake安装脚本?

CMake 用户字符串输入

CMake 在 add_library 的链接阶段末尾添加 -ldl