什么是CMakeCache.txt,为什么它会覆盖我的变量 [英] What is CMakeCache.txt and why it overrides my variables

查看:341
本文介绍了什么是CMakeCache.txt,为什么它会覆盖我的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我编辑CMakeLists.txt之后使用cmake时,某些变量不会被加载。如果我用CACHE STRING定义了某些内容,那将不允许我在不强制更改或删除缓存的情况下进行更改。

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.

那么,为什么我们有了这个CMakeCache.txt文件。甚至需要吗?

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

推荐答案

是的,当然是必需的。 CMake在构建期间由于更改CMakeList文件而重新运行自身时或在 make rebuild_cache 时使用缓存。

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.

使用CMake的标准工作流程如下:

The standard worflow for using CMake is as follows:


  1. 在一个空的二进制目录中运行CMake以生成项目的初始版本&缓存

  2. 在CMake GUI或 ccmake 或类似工具中,检查初始运行时设置的缓存变量,并根据需要修改它们

  3. 重新运行CMake(或者如果您的UI提供,则仅运行其配置步骤)。

  4. 重复步骤2和3,直到您满意为止。使用配置

  5. 如果您仅在3中运行Configure,请运行Generate

  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.

要使以上各项正常工作,缓存中的用户更改必须优先于CMakeLists.txt中指定的默认缓存值。否则,从第二点开始的用户更改将在下一次配置时丢失,并再次由项目指定的默认值覆盖。

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.

这就是CMake命令 set的原因(var ... CACHE)请勿修改缓存变量 var (如果已存在)。通常,您的项目应将设置缓存视为提供用户可调整的默认值。

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:


  • FORCE 添加到集中命令,或

  • 使用 set 而不使用 CACHE 来设置非缓存变量。非缓存变量优先于同名的缓存变量。

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

这篇关于什么是CMakeCache.txt,为什么它会覆盖我的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆