cmake Variables and Properties Local Variable


set(my_variable "the value is a string")

By default, a local variable is only defined in the current directory and any subdirectories added through the add_subdirectory command.

To extend the scope of a variable there are two possibilities:

  1. CACHE it, which will make it globally available

  2. use PARENT_SCOPE, which will make it available in the parent scope. The parent scope is either the CMakeLists.txt file in the parent directory or caller of the current function.

    Technically the parent directory will be the CMakeLists.txt file that included the current file via the add_subdirectory command.