The following is strange!
macro(foo in1)
message("FIRST" ${in1})
message("OPtional1:" ${ARGV1})
message("OPtional2:" ${ARGV2})
if( NOT ${ARGV1} AND ${ARGV2} )
message("IF")
else()
message("ELSE")
endif()
endmacro()
Running the following:
foo("gaga" false true)
(SHOULD GIVE “IF”)
WORKS!
BUT
foo("gaga" false)
(SHOULD GIVE “ELSE” because the second optional argument is FALSE!)
Results in error:
cmake:126 (if):
given arguments: "NOT" "false" "AND"
Unknown arguments specified
Is this a bug??
The following works:
if( NOT "${ARGV1}" AND "${ARGV2}" )
message("IF")
else()
message("ELSE")
endif()
WHY???
THANKS For any help!
(With functions it works)
In macros, ARGV1 etc. are not real CMake variables. Instead,
${ARGV1}is the string replacement from the macro call. That means that your call with only one argument expands towhich explains the error message. By enclosing the string expansions in quotes, you now have
giving the IF command an empty string to evaluate. Other solutions: use the ${ARGC} string replacement to decide whether the argument is present before trying to expand ARGV2. Or, as you’ve mentioned, switch to a function instead of a macro. In a function, ARGV1 is an actual CMake variable, and you can write it more sensibly, even without the ${} expansion at all:
Reference:
http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command:macro