Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 508
Alex Avatar asked Mar 18 '14 14:03

Alex


People also ask

How do I add options to CMake?

cmake -G %1 -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=ON -DBUILD_TESTS=ON .. You have to enter all your command-line definitions before including the path. @Ébe-isaac If you want to explicitly turn an option OFF just use -DOPTION=OFF .

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

How do I use environment variables in CMake?

Use the syntax $ENV{VAR} to read environment variable VAR . To test whether an environment variable is defined, use the signature if(DEFINED ENV{<name>}) of the if() command. For general information on environment variables, see the Environment Variables section in the cmake-language(7) manual.

How do you set a variable in Cmakelists txt?

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. cmake .


3 Answers

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.

like image 187
Simon Avatar answered Oct 19 '22 21:10

Simon


I had a similar problem and was able to solve it using a slightly different approach.

I needed some compilation flags to be added in case cmake was invoked with an option from the command line (i.e cmake -DUSE_MY_LIB=ON). If the option was missing in the cmake invocation I wanted to go back to default case which was turning the option off.

I ran into the same issues, where the value for this option was being cached between invocations:

cmake -DUSE_MY_LIB=ON .. #invokes cmake and puts USE_MY_LIB=ON in CMake's cache.
cmake ..                 #invokes cmake with the cached option ON, instead of OFF

The solution I found was clearing the option from within CMakeLists.txt after the option was used:

option(USE_MY_LIB "Use MY_LIB instead of THEIR_LIB" OFF) #OFF by default
if(USE_MY_LIB)
    #add some compilation flags
else()
    #add some other compilation flags
endif(USE_MY_LIB)
unset(USE_MY_LIB CACHE) # <---- this is the important!!

Note: The unset option is available since cmake v3.0.2

like image 25
Daniel Avatar answered Oct 19 '22 22:10

Daniel


Try this, it works for me

unset(USE_MY_LIB CACHE)
like image 2
Karl Avatar answered Oct 19 '22 22:10

Karl