cmake if else with option

Alex picture Alex · Mar 18, 2014 · Viewed 84.2k times · Source

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?

Answer

Simon picture Simon · Mar 18, 2014

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.