[CMake] comparing strings

Shaun Williams shaunewilliams at gmail.com
Thu Feb 14 16:44:32 EST 2013


Hi,

I learned something very valuable today about cmake after getting an
unexpected result with STREQUAL:

set(foo bar)
...
set(baz foo)
...
if("${baz}" STREQUAL "bar") # This evaluates to true.
...

I expected it to be false, because I was trying to get baz's value ("foo")
to compare with "bar".  I thought that parameters explicitly quoted were
treated as strings.

Then I learned that the interpreter is the only one that sees quotes around
parameters, for the sole purpose of string interpolation and preventing
whitespace from splitting a parameter.  Cmake commands do not see these
quotes. Therefore, STREQUAL can never know the difference between "baz" and
baz.

So, STREQUAL treats a parameter as a variable name if it is defined, but as
a string value if it is not.  (I verified this in cmIfCommand.cxx)

Is this quote behavior well-known among cmake users?  For others like me,
I'd like this behavior to be emphasized in the cmake docs for STREQUAL.

Happy Valentine's.

Shaun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20130214/4a32ceb4/attachment.htm>


More information about the CMake mailing list