[CMake] comparing strings

Shaun Williams shaunewilliams at gmail.com
Mon Feb 18 11:34:50 EST 2013


Thanks, I will be using this method:

if (myvar MATCHES "^somestring$")
endif()

Do NOT use "${myvar}" because that forces CMake to treat the value of myvar
as a variable name if it exists.  That will give you accidentally
indirection that may point to the value of another string.


On Fri, Feb 15, 2013 at 12:13 PM, Alexander Neundorf <
a.neundorf-work at gmx.net> wrote:

> On Thursday 14 February 2013, Shaun Williams wrote:
> > I'm slowly realizing the gravity of this behavior:
> >
> > if(build_system STREQUAL "windows")
> > ...
> > endif()
> >
> > If someone creates a variable named "windows", then this code will not
> work
> > as intended.
> >
> > I'm starting to convert our scripts to use this hopefully foolproof
> > alternative:
> >
> > string(COMPARE EQUAL "${build_system}" windows _cmp)
> > if (_cmp)
> > ...
> > endif()
> >
> > It will make it ugly to create a nested if else block, but it'll work.
>
> You can do
> if("${myvar}" MATCHES "somestring")
> or more strict
>
> if("${myvar}" MATCHES "^somestring$")
>
> Alex
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20130218/e66a7238/attachment.htm>


More information about the CMake mailing list