What is the difference between “option” and “set CACHE BOOL” for a CMake variable?

后端 未结 2 1361
闹比i
闹比i 2020-12-28 12:16

Is there any difference between the following two?

set(FOO true CACHE BOOL \"description\")

option(FOO \"description\" ON)

Documentation:

相关标签:
2条回答
  • 2020-12-28 12:36

    In your example, there is no difference. But there can be differences:

    • Option has a default value of OFF.
    • You can make an option dependent on other options with CMakeDependentOption
    • Set accepts types other than BOOL, especially ;-lists.

    Additionally, when you use an option you are indicating to your user that it can be set safely. Setting internal variables from the outside might subtly break the script with invalid values.

    0 讨论(0)
  • 2020-12-28 12:51

    Stumbled on this question, and thought I added an update.

    As explained here, the option command does not create a CACHE variable if a normal variable with the same name exists. This behavior was introduced in version 3.13 (where it is the default). On the other hand, setting a CACHE variable named FOO when FOO exists as a normal variable, will yield two copies of FOO, a normal variable and a CACHE one.

    Therefore, whether to use option(FOO "" ON) or set(FOO ON CACHE BOOL "") may also depend on what you want your configuration system to do when a variable with the same name has already been defined upstream.

    Note: the two still behave the same way if FOO has been defined upstream in the cache. The difference is only if the upstream is a normal variable.

    0 讨论(0)
提交回复
热议问题