cmake调用动态宏名称 [英] cmake invoking dynamic macro name

查看:79
本文介绍了cmake调用动态宏名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个宏名称,例如:

I have a two macro names, for example:

macro(my_macro1)
# stuff only to use in this macro
endmacro()

macro(my_macro2)
# stuff only to use in this macro
endmacro()

我想基于变量名动态调用宏,例如:

I'd like to dynamic call the macros based a variable name, for example:

if (...)
set (ver 1)
else ()
set (ver 2)
endif ()
my_macro${ver} # this is my idea

有什么帮助吗?

推荐答案

正如@Tsyvarev所说,CMake不支持动态函数名。因此,这里有一些替代方法:

As @Tsyvarev has commented CMake doesn't support dynamic function names. So here are some alternatives:

简单方法

macro(my_macro ver)
    if(${ver} EQUAL 1)
        my_macro1()
    elseif(${ver} EQUAL 2)
        my_macro2()
    else()
        message(FATAL_ERROR "Unsupported macro")
    endif()
endmacro()

set(ver 1)
my_macro(ver)
set(ver 2)
my_macro(ver)

A call()函数实现

A call() Function Implementation

在<一个href = https://stackoverflow.com/questions/23615436/invoke-macro-with-variable-name-in-cmake> @Fraser工作这是一个更通用的调用()函数实现:

Building on @Fraser work here is a more generic call() function implementation:

function(call _id)
    if (NOT COMMAND ${_id})
        message(FATAL_ERROR "Unsupported function/macro \"${_id}\"")
    else()
        set(_helper "${CMAKE_BINARY_DIR}/helpers/macro_helper_${_id}.cmake")
        if (NOT EXISTS "${_helper}")
            file(WRITE "${_helper}" "${_id}(\$\{ARGN\})\n")
        endif()
        include("${_helper}")
    endif()
endfunction()

set(ver 1)
call(my_macro${ver})
set(ver 2)
call(my_macro${ver})

这篇关于cmake调用动态宏名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆