Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is CMakeCache.txt and why it overrides my variables

Tags:

As I am using cmake after editing CMakeLists.txt some variables wouldn't be loaded. If I had something defined with CACHE STRING it wouldn't let me to change it without forcing it or deleting cache.

So then why we have this CMakeCache.txt file. Is it even needed?

like image 535
oneat Avatar asked Feb 10 '17 13:02

oneat


People also ask

Can I delete CMakeCache txt?

Deleting CMakeCache. txt should not affect any binaries, a full rebuild is not triggered. Otherwise you might have a local variable passed on before being cached which then leads to inconsistent runs. The regeneration of the project can trigger rebuilds if the configuration is different to the previous one.

How do you set a variable in CMakeLists txt?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake .

Where are CMake variables stored?

Cache variables are stored in the CMake cache file, and are persisted across CMake runs.

How do you pass a variable in CMake?

Options and variables are defined on the CMake command line like this: $ cmake -DVARIABLE=value path/to/source You can set a variable after the initial `CMake` invocation to change its value. You can also undefine a variable: $ cmake -UVARIABLE path/to/source Variables are stored in the `CMake` cache.


1 Answers

Yes, it's certainly needed. CMake uses the cache when it's re-running itself during a build because a CMakeList file changed, or when you make rebuild_cache. It also loads the cache at start of a normal configure run.

The standard workflow for using CMake is as follows:

  1. Run CMake in an empty binary directory to generate the initial version of the project & cache
  2. In CMake GUI or ccmake or similar, inspect the cache variables set up by the initial run and modify them as you see fit.
  3. Re-run CMake (or just its Configure step if your UI offers that).
  4. Repeat steps 2&3 until you're satisfied with the configuration
  5. If you were only running Configure in 3, run Generate

You now have a buildsystem configured according to your taste.

For the above to work, user changes in the cache must take precedence over default cache values specified in CMakeLists.txt. Otherwise, the user changes from point 2 would be lost at next configure, overwritten by the project-specified defaults again.

That's why CMake commands set(var ... CACHE) do not modify the cache variable var if it already exists. Normally, your project should treat setting up the cache as providing user-tweakable defaults.

If you really need to override user choices in your project, you can:

  • add FORCE to the set command, or
  • use set without CACHE to set non-cache variables. Non-cache variables take precedence over cache variables of the same name.
like image 73
Angew is no longer proud of SO Avatar answered Oct 10 '22 04:10

Angew is no longer proud of SO