As far as I know, CMake only comes with graphical cache editors. However, I need to edit some cache variables from a shell script. One way is to directly edit CMakeCache.txt
but this is not considered safe. Or is it? If not, what is the general practice of editing a cache variable from the command line?
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.
In a CMake script you can only change existing Cache entries if you use the set(... CACHE ... FORCE) syntax. This behavior is made use of e.g. by CMake itself, because it normally does not force Cache entries itself and therefore you can pre-define it with another value.
The CMake cache may be thought of as a configuration file. The first time CMake is run on a project, it produces a CMakeCache. txt file in the top directory of the build tree. CMake uses this file to store a set of global cache variables, whose values persist across multiple runs within a project build tree.
In CMake, every variable is a string. You can substitute a variable inside a string literal by surrounding it with ${} . This is called a variable reference. Modify hello.txt as follows: message("Hello ${NAME}!") # Substitute a variable into the message.
You may call cmake
:
-D
option(s) for set/modify cache variables, or-U
option(s) for remove variables from the cache.Running cmake
will cause the project to be reconfigured, but this should be an ultimate purpose of any cache modification.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With