[CMake] Question about sending a macro a list of items

Brad King brad.king at kitware.com
Thu Jul 28 13:45:55 EDT 2005


alaterale at elitemail.org wrote:
> Hi again,
> 
> I have a quick question about the use of MACRO that I need answered.  I
> want to be able to send a list of names to a MACRO that I made so I can
> use that list in a FOREACH statement or in other statements.  How do I
> do this?  It only seems to receive the first item in the list.
> 
> SET(ITEMS item1 item2 item3 item4)
> 
> MACRO(print itemlist)
>     MESSAGE("${itemlist}")
>     FOREACH(item ${itemlist})
>        MESSAGE("${item}")
>     ENDFOREACH(item)
> ENDMACRO(print)
> print(${ITEMS})
> 
> I would expect to see item1;item2;item3... and then a vertical list of
> the same names.  But I only ever see the first item in the list.  Can
> anyone help me?  I didn't notice that this was happening until much
> later than when I wrote the code and I need to be able to do this.

Since the MACRO you've declared takes only one argument you have to use 
it to pass the NAME of the variable containing the list.  Then you can 
expand it inside the macro:

SET(ITEMS item1 item2 item3 item4)

MACRO(print itemlist)
     MESSAGE("${${itemlist}}")
     FOREACH(item ${${itemlist}})
        MESSAGE("${item}")
     ENDFOREACH(item)
ENDMACRO(print)
print(ITEMS)

-Brad


More information about the CMake mailing list