[CMake] Confusion with range-based FOREACH logic

Robert Maynard robert.maynard at kitware.com
Mon Jan 29 08:24:48 EST 2018


Hi,

Looking at the documentation for 'RANGE' indicates that it is
inclusive on both ends (but poorly), which as you point out is not
what most people will expect. To solve your problem you can subtract 1
from len before you loop to have the behavior you want.


On Tue, Jan 23, 2018 at 12:27 PM, Robert Dailey
<rcdailey.lists at gmail.com> wrote:
> I am doing the following:
>
> ```
> set( CS_THIRD_PARTY_REFERENCES
>     Iconic.Zip ${CMAKE_CURRENT_SOURCE_DIR}/DotNetZip/Iconic.Zip.dll
> )
>
> if( CS_THIRD_PARTY_REFERENCES )
>     list( LENGTH CS_THIRD_PARTY_REFERENCES len )
>     message( STATUS "len: ${len}" )
>     foreach( i RANGE 0 ${len} 2 )
>         message( STATUS "i: ${i}" )
>         list( GET CS_THIRD_PARTY_REFERENCES ${i} assembly_name )
>         math( EXPR i "${i}+1" )
>         list( GET CS_THIRD_PARTY_REFERENCES ${i} assembly_path )
>         set_property( TARGET ${target_name} PROPERTY
>             VS_DOTNET_REFERENCE_${assembly_name} ${assembly_path}
>         )
>         message( STATUS "i: ${i}" )
>         message( STATUS "assembly_name: ${assembly_name}" )
>         message( STATUS "assembly_path: ${assembly_path}" )
>     endforeach()
> endif()
> ```
>
> I am finding that the foreach() loops 2 times when ${len} is 2. What I
> expect during the 2nd iteration is that it does ${i}+STEP (0+2) which
> would equal 2, and so long as 2<${len} (2<2), then it would continue
> to iterate. In this case, it should stop after the first iteration.
> I'm thinking that this should work like a C++ for loop. Is this not
> the case? How should I be iterating over a list of 2 times at a time?
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake


More information about the CMake mailing list