On 04-Sep-14 09:58, Clark Wang wrote:
On Thu, Sep 4, 2014 at 1:44 PM, Clark Wang <[email protected]
<mailto:[email protected]>> wrote:
On Thu, Sep 4, 2014 at 1:36 PM, Chuck Atkins
<[email protected] <mailto:[email protected]>> wrote:
Hi Clark
The expression inside the if statement has it's variables
dereferenced before evaluating and the non-variables are
treated as constant expressions. In this case, a resolves to
"b", b resolves to "c", and c is not a variable so it's
treated as the constant expression "c". Thus
if(a STREQUAL b OR a STREQUAL c)
gets evaluated as
if( ("b" STREQUAL "c") OR ("b" STREQUAL "c") )
Thanks a lot. It's clear now. But is there a way to check if the
value of the variable a equals to "b" or "c"?
Found
http://stackoverflow.com/questions/19982340/cmake-compare-to-empty-string-with-strequal-failed
which suggests to use if(a MATCHES "^b$").
The core of the problem is that command "if(... STREQUAL ...)" has
ambiguous syntax `<variable|string>`. So the result depends on what the
left/right side is (if variable -> use variable value, if not, use
string as-is).
Command `if(a MATCHES ...)` has the same flaw: `if(<variable|string>
MATCHES regex)`:
# MYSTRING undefined
if(MYSTRING MATCHES "^MYSTRING$")
# go here even MYSTRING is not defined (string "MYSTRING" used)
endif()
set(MYSTRING "B")
set(A "MYSTRING")
if("${A}" MATCHES "^MYSTRING$")
# do *not* go here even A is MYSTRING (use *variable* MYSTRING)
endif()
One of the solution that doesn't suffer from ambiguity is
`string(COMPARE EQUAL <string1> <string2> <output variable>)`:
string(COMPARE EQUAL "${MYSTRING}" "MYSTRING" is_equal)
if(is_equal)
# only if MYSTRING defined and is "MYSTRING"
endif()
string(COMPARE EQUAL "${A}" "MYSTRING" is_equal)
if(is_equal)
# only if A defined and is a string "MYSTRING" (works even if
MYSTRING variable defined)
endif()
--
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