我有一个像这样的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
,而实际上应该找到它。我怎样才能像我想的那样即时传递一个列表?
答案 0 :(得分:3)
这是因为宏的参数和ARGN
之类的特殊值不是通常的CMake意义上的变量。它们是字符串替换,就像c预处理器用宏来做的那样。
您可以将输入参数复制到变量,然后将该变量传递给列表find:
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