[cmake-developers] if (FOO == BAR) ...

Alexander Neundorf neundorf at kde.org
Sat Mar 23 10:22:48 EDT 2013


On Thursday 21 March 2013, Matthew Woehlke wrote:
> On 2013-03-21 16:55, David Cole wrote:
> > I almost always do one of these for string compare to a CMake variable
> > 
> > value:
> >      if("${var}" STREQUAL "some string constant")
> >      if("${var}" STREQUAL "${some_other_variable}")
> > 
> > However, this is only because I am almost always certain that ${var}
> > does not evaluate to the name of yet another CMake variable.
> > If it did, I would get unintended results.
> > 
> > So I wouldn't say it's necessarily a best-practice. It's "close enough"
> > for many lines of code I've written, but a monkey-wrench could easily
> > be thrown at it.
> > 
> > Does forcing the if(VARIABLE usage rather than the if("string" usage
> > make things work all the time? (Now I've thought about it too hard,
> > and I can't remember if this works all the time or not...)
> > 
> >      set(x "${var}")
> >      if(x STREQUAL "some string constant")
> 
> I think this would be the only way to be 100% safe. 

I think the right side also needs to be a variable, there might be a variable 
named "some string constant" (ok, usually there isn't).

Alex



More information about the cmake-developers mailing list