将常量列表传递到 cmake 宏的方法?

发布于 2024-12-07 18:15:49 字数 352 浏览 1 评论 0原文

我有一个像这样的 CMake 宏:

macro( foo a b )
  list( FIND b ${a} is_found )
endmacro()

我尝试像这样调用它:

foo( "test" foo;bar;test )

这不起作用。这也不起作用:

foo( "test" "foo;bar;test" )

在所有情况下,我得到的 is_found 等于 -1,而实际上它应该已经找到了。我怎样才能像我想要的那样即时传递列表?

I have a CMake macro like so:

macro( foo a b )
  list( FIND b ${a} is_found )
endmacro()

And I try to call it like so:

foo( "test" foo;bar;test )

This doesn't work. Also this does not work:

foo( "test" "foo;bar;test" )

In all cases I get is_found equal to -1, when in fact it should have been found. How can I pass in a list on-the-fly like I want to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性不性感 2024-12-14 18:15:49

发生这种情况是因为宏的参数和特殊值(例如 ARGN)不是通常 CMake 意义上的变量。它们是字符串替换,就像 c 预处理器对宏所做的那样。

您可以将输入参数复制到变量,然后将该变量传递到列表查找:

macro( foo a )
  set( b "${ARGN}" )
  list( FIND b "${a}" is_found )
endmacro()

因此,以下所有变体都有效:

foo(test foo bar test foo )
foo("test" foo bar test foo )
foo(test foo;bar;test;foo )
foo("test" foo;bar;test;foo )
foo(test "foo;bar;test;foo" )
foo("test" "foo;bar;test;foo" )

更新,更通用的版本 - 在由“NEXTLIST”单词分隔的多个列表中搜索:

macro( foo a )
  set( is_found )
  set( foo_current_list )
  foreach( arg ${ARGN} )
    if( arg STREQUAL "NEXTLIST" )
      list( FIND foo_current_list "${a}" foo_is_found )
      list( APPEND is_found ${foo_is_found} )
      set( foo_current_list )
    else()
      list( APPEND foo_current_list ${arg} )
    endif()
  endforeach()
  list( FIND foo_current_list "${a}" foo_is_found )
  list( APPEND is_found ${foo_is_found} )
  unset( foo_is_found )
  unset( foo_current_list )
endmacro()

foo (test bar bar bar NEXTLIST foo test NEXTLIST test test x test)
message( "${is_found}" ) #-1;1;0

This happens because parameters of a macro and special values such as ARGN are not variables in the usual CMake sense. They are string replacements much like the c preprocessor would do with a macro.

You can copy input arguments to the variable and next pass that variable to list find:

macro( foo a )
  set( b "${ARGN}" )
  list( FIND b "${a}" is_found )
endmacro()

As result all the following variants work:

foo(test foo bar test foo )
foo("test" foo bar test foo )
foo(test foo;bar;test;foo )
foo("test" foo;bar;test;foo )
foo(test "foo;bar;test;foo" )
foo("test" "foo;bar;test;foo" )

Update, more generic version - search in several lists separated by "NEXTLIST" word:

macro( foo a )
  set( is_found )
  set( foo_current_list )
  foreach( arg ${ARGN} )
    if( arg STREQUAL "NEXTLIST" )
      list( FIND foo_current_list "${a}" foo_is_found )
      list( APPEND is_found ${foo_is_found} )
      set( foo_current_list )
    else()
      list( APPEND foo_current_list ${arg} )
    endif()
  endforeach()
  list( FIND foo_current_list "${a}" foo_is_found )
  list( APPEND is_found ${foo_is_found} )
  unset( foo_is_found )
  unset( foo_current_list )
endmacro()

foo (test bar bar bar NEXTLIST foo test NEXTLIST test test x test)
message( "${is_found}" ) #-1;1;0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文