15

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?

2
  • CMake generates a CMakeCache.txt file in the build folder. stackoverflow.com/questions/42160117/… Commented Nov 6, 2018 at 2: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..." Commented Dec 30, 2019 at 23:03

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
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.
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?
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.
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?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.