Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for CMake Variables

Tags:

cmake

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!

like image 627
asumang Avatar asked Aug 04 '15 11:08

asumang


People also ask

How can I see CMake variables?

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.

Where are CMake variables set?

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.

How do I print a variable value in CMake?

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.

What is CMake Strequal?

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()


1 Answers

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.

like image 192
user268396 Avatar answered Nov 12 '22 15:11

user268396