[CMake] foreach(loop_var RANGE total)

David Cole david.cole at kitware.com
Fri Sep 12 08:25:26 EDT 2008


Add this line after the list LENGTH call:math(EXPR list_max_index
${list_count}-1)

and then use list_max_index instead of list_count in the foreach loop.

The documentation (cmake --help-command foreach) is quite clear that the one
arg form of the range command loops from 0 to total including total...


HTH,
David


On Fri, Sep 12, 2008 at 5:56 AM, Nicolas Desprès
<nicolas.despres at gmail.com>wrote:

> Hi,
>
> I'm using cmake 2.6.0.
>
> I'm trying the foreach(loop_var RANGE total) syntax and I find its
> behavior quiet surprising since it is something equivalent to:
>
> for (int loop_var = 0; loop_var <= total; ++loop_var)
>  ;
>
> Most of the time (specially when you iterate over a list) you want to
> test for 'loop_var < total'.
>
> For instance you can't write code like that:
>
> list(LENGTH list list_count)
>
> foreach(i RANGE ${list_count})
>
>    list(GET list ${i} x)
>
> endforeach(i)
>
> list(GET always fail on the last iteration.
>
> Cheers,
>
> --
> Nicolas Desprès
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20080912/cb6a8ed5/attachment-0001.htm>


More information about the CMake mailing list