[CMake] if defined and boolean value

Eric Noulard eric.noulard at gmail.com
Sun Jul 14 07:58:27 EDT 2013


2013/7/14 Alexander Neundorf <a.neundorf-work at gmx.net>:
> On Saturday 13 July 2013, Victor Aurélio Santos wrote:
>> Hi,
>>
>> I'm trying to evaluate a expression "if variable X is defined and Y is ON":
>> tried this:
>> if (defined ${GETTEXT_LIB-NOTFOUND} and ${ENABLE_NLS})
>
> You probably used "DEFINED", no "defined", right ?

As Alexander noticed keywords in if expression are case sensitive
so it should be

if (DEFINED ${GETTEXT_LIB-NOTFOUND} AND ${ENABLE_NLS})

defined --> DEFINED
and --> AND

then if you test whether if the GETTEXT_LIB-NOTFOUND var has been defined then
the expression should be.

if (DEFINED GETTEXT_LIB-NOTFOUND AND...)

you don't want to evaluate "GETTEXT_LIB-NOTFOUND" because in this case
you would test whether if **the value** of this
var corresponds to a DEFINED variable.
In the very same way testing whether if ENABLE_NLS is true leads to:

if (DEFINED GETTEXT_LIB-NOTFOUND AND ENABLE_NLS)

Basically what you said:
"if variable X is defined and Y is ON"
translates into:

if (DEFINED X AND  Y)

Try this code snippet:

if(DEFINED A AND B)
  message(STATUS "A is defined and B is TRUE")
elseif (DEFINED A)
  message(STATUS "A is defined but B is not TRUE")
elseif (B)
  message(STATUS "A is NOT defined but B is TRUE")
else ()
  message(STATUS "A is NOT defined and B is FALSE")
endif()

and make the definition/value of A and B vary and you'll see the result.
--
Erk
L'élection n'est pas la démocratie -- http://www.le-message.org


More information about the CMake mailing list