I have the following CMake code snippet in my CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR) message(STATUS "Before setting - ${MY_VARIABLE}") # first check if(NOT DEFINED ${MY_VARIABLE}) set(MY_VARIABLE true) endif(NOT DEFINED ${MY_VARIABLE}) message(STATUS "After setting - ${MY_VARIABLE}") # second check if(NOT DEFINED ${MY_VARIABLE}) message(STATUS "What - ${MY_VARIABLE}") endif(NOT DEFINED ${MY_VARIABLE})
The output from CMake configuration is:
-- The C compiler identification is GNU 4.8.4 -- The CXX compiler identification is GNU 4.8.4 -- Check for working C compiler: /usr/local/bin/cc -- Check for working C compiler: /usr/local/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/local/bin/c++ -- Check for working CXX compiler: /usr/local/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Before setting - -- After setting - true -- What - true -- Configuring done -- Generating done -- Build files have been written to: /tmp/build
Question: Why does the second check for variable definition report the variable is not defined even though it is defined? Surprisingly, the value of the variable is also printed correctly!
Another way to view all cmake's internal variables, is by executing cmake with the --trace-expand option. This will give you a trace of all . cmake files executed and variables set on each line.
You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache.
Run CMake and have a look at the cache with the ccmake GUI tool. Then you'll get all the variables. Or run CMake with -LH then you will get all variables printed after configuration.
According to the CMake documentation, the STREQUAL comparison is allowed to take either a VARIABLE or a STRING as either parameter. So, in this example below, the message does NOT print, which is broken: set( FUBARTEST "OK" ) if( FUBARTEST STREQUAL "OK" ) message( "It Worked" ) endif()
There is a difference between: if(NOT DEFINED VAR_NAME)
and if(NOT DEFINED ${VAR_NAME})
The first one refers to the variable and the other to its content.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With