Issue
Fltk is found, but test not passed, why?
define a simple macro
macro(assert TEST COMMENT)
message(${TEST})
if(NOT ${TEST})
message("Assertion failed: ${COMMENT}")
endif()
endmacro()
# use the macro
find_library(FLTK_LIB fltk)
assert(${FLTK_LIB} "Unable to find library fltk")
Output:
/usr/lib/x86_64-linux-gnu/libfltk.so
Assertion failed: Unable to find library fltk
Solution
if( NOT ${TEST} )
You are expanding TEST
here, i.e. you are basically saying...
if( NOT "/usr/lib/x86_64-linux-gnu/libfltk.so" )
CMake docs on if
state that:
if(<string>)
A quoted string always evaluates to false unless:
- The string's value is one of the true constants [...]
So the default is false, and true constants are:
[...] 1, ON, YES, TRUE, Y, or a non-zero number.
Your string is neither, so it's false, so your assertion always triggers.
If you were in a function
, the solution would be to just write...
if( NOT TEST )
...because that wouldn't test the string, but the variable, on which the documentation states...
if(<variable>)
True if given a variable that is defined to a value that is not a false constant. False otherwise, including if the variable is undefined.
So the default is true, unless your variable is undefined or one of the false constants...
0, OFF, NO, FALSE, N, IGNORE, NOTFOUND, the empty string, or ends in the suffix -NOTFOUND
Unfortunately you have written your assertion as a macro, not a function. Unfortunately, because...
Note that macro arguments are not variables.
At which point I, personally, heed my C/C++ programmer genes of "macros are evil" and would suggest you turn your assert()
into a function
and write if( NOT TEST )
.
Answered By - DevSolar