[CMake] doing simple math in cmake or useless foreach( RANGE )

Marcel Loose loose at astron.nl
Wed May 20 03:40:53 EDT 2009


Hi Nicolas,

I second your opinion. I think CMake is a bit ambiguous in this sense.
List indices are zero-based, which clearly shows a C++ implementation
background. On the other hand, range intervals are closed both at the
beginning and the end, which is not like C++, nor like, e.g., Python,
but more like one-based languages like Fortran and Pascal.

In cases like this I revert to something like this:

LIST(LENGTH mylist count)
WHILE(index LESS count)
  LIST(GET ${mylist} ${index} item)
  MATH(EXPR index "${index}+1")
WHILE(index LESS count)

Cumbersome, but it works.

Best regards,
Marcel Loose.

-----Original Message-----
From: Nicolas Desprès <nicolas.despres at gmail.com>
To: Hendrik Sattler <post at hendrik-sattler.de>
Cc: cmake at cmake.org
Subject: Re: [CMake] doing simple math in cmake or useless
foreach( RANGE )
Date: Tue, 19 May 2009 23:38:05 -0300

On Tue, May 19, 2009 at 6:29 PM, Hendrik Sattler
<post at hendrik-sattler.de> wrote:

>
> If that's not present, I do not understand how
> LIST( LENGTH MYLIST COUNT )
> FOREACH ( index RANGE ${COUNT} )
> list ( GET MYLIST ${index} entry )
> ....
>
> will address non-present indexes in the list. I would the above statement
> cover the range from 0 to COUNT-1 but it also cover COUNT itself like said in
> the documentation. Does that actually make sense as the variable in the
> documentation is called "total" but the loop count is "total+1"?
>

I also experienced this problem and I don't like this behavior. I'm
more use to the traditional

for (i = 0; i < n; ++i)

instead of <=





More information about the CMake mailing list