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

add_directory( subdir )

However, when I run cmake, I get

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!

I finally got back to this. I was able to fix it. I did have to set the scope to the parent scope for the cache variable for it to fully work