Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need FORCE to override a CMake option?

Tags:

cmake

I want to override an option, which formely was set to OFF. I am using

SET(USE_OPTION ON CACHE BOOL "Override option" FORCE)

Just for my curiosity: Why do I need 'FORCE' (without it, the set() does not work)

like image 910
katang Avatar asked Sep 20 '16 08:09

katang


Video Answer


2 Answers

Options are variables meant to be changeable by the user after the configuration step and before the actual build enviroment generation through tools like the cmake-gui, ccmake or through the -D command line switch.

That's why they are cached in the first place. The choice the user has done has to be persisted. So

option(USE_OPTION "My option" ON)

is an equivalent to

set(USE_OPTION ON CACHE BOOL "My option")

So to change or force an "user definable value" / cached variable you have to FORCE it.

Or hide/overwrite it for the current variable scope with a non-cached variable:

set(USE_OPTION ON)

Reference

  • What's the CMake syntax to set and use variables?
like image 168
Florian Avatar answered Sep 28 '22 11:09

Florian


From the docs:

If CACHE is present, then the is put in the cache instead, unless it is already in the cache

I assume the previous option was also CACHE.

If FORCE is specified, the value of the cache variable is set, even if the variable is already in the cache.

So, if you don't specify FORCE it doesn't get added to the cache as per above.

like image 38
Hatted Rooster Avatar answered Sep 28 '22 10:09

Hatted Rooster