[CMake] set(a b); set(b c); if(a STREQUAL b OR a STREQUAL c) ...

Anders Lindgren andlind at gmail.com
Thu Sep 4 10:09:08 EDT 2014


As other has pointed out, this is caused by "if" interpreting its arguments
as variables, if there is a variable with that name, and quoting the value
don't help.

However, by embedded both argument in some extra text, you can make sure
that the argument don't form a valid variable name. To apply this idea to
your original example:

    set(a b)
    set(b c)
    if(">${a}<" STREQUAL ">b<" OR ">${a}<" STREQUAL ">c<")
      message("true")
    endif()

Another thing you can do to ensure that you don't fall into this trap is to
use distinct variable names. I.e. use variable names that don't naturally
occur in other situations in you CMakeLists:s. In your example "b" is used
both as the name of a variable and as a value.

    -- Anders Lindgren



On Thu, Sep 4, 2014 at 7:01 AM, Clark Wang <dearvoid at gmail.com> wrote:

> Hi,
>
> I don't understand why the following code would not print "true" (tested
> with cmake-3.0):
>
>   set(a b)
>   set(b c)
>   if(a STREQUAL b OR a STREQUAL c)
>     message("true")
>   endif()
>
> From my understanding, no matter how magic the if command interprets its
> arguments, one of the expressions (a STREQUAL b) and (a STREQUAL c) must be
> true but I'm wrong. Anyone has a reasonable explanation?
>
> Thanks.
>
> -clark
>
> --
>
> 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:
> http://public.kitware.com/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20140904/05645868/attachment.html>


More information about the CMake mailing list