12

What is CMake cache?

I'm reading the cmake manual and have occasionally come across the term cmake cache. For instance this paragraph:

-C <initial-cache> Pre-load a script to populate the cache.

When cmake is first run in an empty build tree, it creates a CMakeCache.txt file and populates it with customizable settings for the project. This option may be used to specify a file from which to load cache entries before the first pass through the project’s cmake listfiles. The loaded entries take priority over the project’s default values. The given file should be a CMake script containing SET commands that use the CACHE option, not a cache-format file.

What is this cache?
Are there different types of cache?
Or would a better question be: what is cache in general?

Also, what is the importance of cache?
And are there certain caveats when dealing with cache?
For example, does the cache reset when you turn restart your computer?

Ivan_a_bit_Ukrainivan
  • 808
  • 1
  • 10
  • 27
John DeBord
  • 638
  • 1
  • 5
  • 17
  • CMake generates a `CMakeCache.txt` file in the build folder. https://stackoverflow.com/questions/42160117/what-is-cmakecache-txt-and-why-it-overrides-my-variables – drescherjm Nov 06 '18 at 02:32
  • "The CMake cache?" "Do you want to know _what it is_, Neo?... The CMake cache is everywhere. It is all around us. [Even now...](https://youtu.be/gDadfh0ZdBM?t=146)" – einpoklum Dec 30 '19 at 23:03

1 Answers1

11

CMake cache refers to a set of persistent variables - persisted in a file named CMakeCache.txt in the build directory. These include things like user configurable options, which define some behavior of your project. For example, you may run cmake -D CMAKE_BUILD_TYPE=Release . in the build directory and find that variable saved in CMakeCache.txt.

CMake only has one type of cache. It is created and saved into CMakeCache.txt after the first configuring the build, and under normal use does not reset itself.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Raul Laasner
  • 1,475
  • 1
  • 17
  • 30
  • So the CMake cache shouldn't be used to store package-detection data, as this could result in stale values if the user installs an additional package then re-runs CMake, right? – Max Barraclough Jun 18 '20 at 22:22
  • I don't fully understand the question. I believe package-detection information is actually stored in CMake cache. If you have a `find_package` statement in a CMake script and the package is found, those variable are saved in the cache. – Raul Laasner Jun 19 '20 at 10:19
  • My real question was this: if I run CMake, then install an optional dependency, then re-run CMake, will it detect that the package has been installed? Or do I need to manually delete the cache? – Max Barraclough Jun 19 '20 at 23:42
  • 1
    I believe yes, CMake will run the `find_package` command every time and make changes to the cache if a new package has been installed. – Raul Laasner Jun 20 '20 at 08:47
  • If I want to build the project Debug type will it work `cmake -D CMAKE_BUILD_TYPE=Debug .`, and if I want to build for both Debug and Release type, then `cmake -D CMAKE_BUILD_TYPE=Debug, Release .` will work? – bim Apr 27 '22 at 09:38
  • `cmake -D CMAKE_BUILD_TYPE=Debug .` will work as expected but specifying both Debug and Release will not work. You would need to build those separately. – Raul Laasner Apr 27 '22 at 14:09