在Cmake中使用变量名调用宏 [英] Invoke macro with variable name in Cmake

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

问题描述

我希望能够做到这一点(如果可能的话(尝试获取伪多态性/函数传递以避免复制粘贴))

I'd like to be able to do this (if possible (trying to get pseudo polymorphism/function passing to avoid copy-paste))

 macro(do_A x)
 endmacro()

 macro(do_B x)
 endmacro()

 macro(C y x)
 do_${x}(y)
 endmacro()

 C(asdf,A)

有没有做这种事情的机制?

Is there any mechanism to do such a thing?

编辑:问起来可能更准确,可以吗将宏作为参数传递给另一个宏?

it may be more accurate to ask, can you pass a macro as an argument to another macro?

推荐答案

我不确定是否有更好的方法,但是可以使用 configure_file include 作为帮助程序。

I'm not sure if there's a better way, but you can use configure_file and include here as helpers.

所以,我如果您创建了一个名为 macro_helper.cmake.in的输入文件,其内容如下所示:

So, if you create an input file called "macro_helper.cmake.in" and have its contents as just the following line:

@MacroName@(MacroArg)

然后,您可以将其配置为每个宏的输出文件,然后简单地 include 输出文件:

Then you can configure this to a per-macro output file, and simply then include the output file:

macro(C MacroArg MacroId)
  set(MacroName do_${MacroId})
  # Need to make MacroArg a "proper" variable since we're in a macro, not a
  # function.  Run 'cmake --help-command macro' for more info.
  set(MacroArg ${MacroArg})
  set(OutputFile ${CMAKE_BINARY_DIR}/helpers/macro_helper_${MacroId}.cmake)
  configure_file(macro_helper.cmake.in ${OutputFile} @ONLY)
  include(${OutputFile})
endmacro()

注意:调用 C 时不希望逗号-只需这样做:

Note: you don't want the comma when invoking C - just do:

C(asdf A)

这篇关于在Cmake中使用变量名调用宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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