I'm confused about CMake's cached variables:
Case 1: CACHE
+ FORCE
set(CMAKE_CXX_FLAGS "myflags" CACHE STRING "" FORCE)
"myflags"
appears in the CMakeCache.txt
file as intended."myflags"
- seems like FORCE
has higher priority than command line -D...="..."
arguments. This is not desired - I would like command line arguments to override "myflags"
.Case 2: only CACHE
set(CMAKE_CXX_FLAGS "myflags" CACHE STRING "")
CMakeCache.txt
file. I want"myflags"
to appear for the first run."myflags"
.Am I correct about my conclusions? Or do "default variables" such as CMAKE_CXX_FLAGS
behave differently?
Is there a way to have "myflags"
written in the CMakeCache.txt
file during the first CMake run (when CMake wasn't run previously in this folder)?
I'd like to set "myflags"
during the first CMake run in the cache, then allow the user to override it using the command line.
If I use FORCE
, the user can't override it via command line.
If I don't use FORCE
, "myflags"
isn't written in the cache file during the first run.
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.
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.
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.
CMake caches variables and settings in the CMakeCache. txt file. When you load a project for the first time, this file is generated in the build directory (cmake-build-debug or cmake-build-release by default) according to the contents of CMakeLists. txt.
This is consistent with the behaviour explained in the documentation:
Normally, set(...CACHE...) creates cache variables, but does not modify them. If FORCE is specified, the value of the cache variable is set, even if the variable is already in the cache. This should normally be avoided, as it will remove any changes to the cache variable’s value by the user.
CMAKE_CXX_FLAGS
is present in the cache even without set(CMAKE_CXX_FLAGS ... CACHE ...)
in your CMakeLists.txt, because CMAKE_CXX_FLAGS
variable is already set during compiler flags initialization.
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