if-statementcmakecmakelists-options

cmake if else with option


I have a problem using option together with if-else statement in cmake.

project(test)

option(TESTE "isso é um teste" OFF)

if(TESTE)
  message("true")
else()
  message("false")
endif()

add_executable(test main.cpp)

It always displays true even if I put OFF in the options, what am I doing wrong?


Solution

  • That's because the value of the option is stored in the cache (CMakeCache.txt).

    If you change the default value in the CMakeLists but the actual value is already stored in the cache, it will just load the value from the cache.

    So to test the logic in your CMakeLists, delete the cache each time before re-running CMake.