Proper way to set a CACHE variable

I have shared library that I use where in its cmake files, sets a number of CACHE variables

the cmake file does something like

SET( VAR OFF CACHE BOOL “Some boolean variable” )
MESSAGE( STATUS "VAR=${VAR} )
in the project using the shared library, the shared librtary is added via add_subdirectory

If I want to make sure a cache variable is set correctly, I know I can require it from the command line when calling cmake.

However, this is a setup set often forgotten. So I figured I should be able to do the following

SET( VAR ON )
add_directory( subdir )
MESSAGE( STATUS “VAR=${VAR}” )

However, when I run cmake, I get
VAR=OFF
VAR=ON

Clearly Im doing something basic and wrong. The hack workaround I have done, is to
SET( VAR ON CACHE BOOL “Some boolean” )
in both places, but this just seems really wrong

1 Like

What is the cmake_minimum_required you have set? I suspect policy CMP0126 may be relevant here.

1 Like

Thank you. Looking at the docs it looks like using the new behavior will fix my issue!