arun
arun

Reputation: 72

Modifying CACHE variable in CMake is not working

I am using CMake 3.10.2 in Windows.

When I set the variable using CACHE like this

SET(ABAQUS_MAJORVERSION 2016)
SET(ABAQUS_MAJORVERSION ${ABAQUS_MAJORVERSION} CACHE STRING "" )

When I change the ABAQUS_MAJORVERSION variable to 2014 in GUI, this change is not updated in CMake. It keeps generating for 2016 version.

Please help in this regard. Thanks in Advance

Edit1:

This is the project structure:

|CMakeLists.txt
|FindABAQUS.cmake
|-project1
|---source1.cpp
|---CMakeLists.txt which has SET(ABAQUS_MAJORVERSION 2016 CACHE STRING "")  
|-project2
|---source2.cpp
|---CMakeLists.txt which has SET(ABAQUS_MAJORVERSION 2016 CACHE STRING "")

I changed the ABAQUS_MAJORVERSION to 2014 in GUI. The ABAQUS_MAJORVERSION became 2014 in CMakeCache.txt file. But when printed with message(${ABAQUS_MAJORVERSION }) it shows 2016

Solution:

example: SET(MAJORVERSION 2016 CACHE STRING "")

One might need to unset all the Include paths and library paths, to take effect of the new version Include path and library paths.

example: UNSET(INCLUDE_PATH CACHE)
         UNSET(LIBRARY_PATH CACHE)

Upvotes: 1

Views: 2739

Answers (1)

starball
starball

Reputation: 51351

It may depend on how you're using (or accidentally not using) the cache variable. You can have a normal variable and cache variable of the same name existing at the same time (which is exactly what you have going on) and still access them both (as per the docs on variable references) using ${var_name} for the regular variable, and $CACHE{var_name} for the cache variable.

This can trip people up because they aren't used to writing the explicit cache form, because usually the following behaviour takes effect:

When evaluating Variable References, CMake first searches the function call stack, if any, for a binding and then falls back to the binding in the current directory scope, if any. If a "set" binding is found, its value is used. If an "unset" binding is found, or no binding is found, CMake then searches for a cache entry. If a cache entry is found, its value is used. Otherwise, the variable reference evaluates to an empty string. The $CACHE{VAR} syntax can be used to do direct cache entry lookups.

I'm guessing this is what's tripping you up.


The following scenario can be another cause of confusion for anyone not aware of its behaviour, but I don't think it's what's tripping you up here.

In the CMake docs for setting cache variables:

Since cache entries are meant to provide user-settable values this does not overwrite existing cache entries by default. Use the FORCE option to overwrite existing entries.

For example, cache variables can be set on the command line with -D var_name:TYPE=VALUE.

Upvotes: 0

Related Questions