0

在命令“project”提示当前 OP后首次运行的 cmake 设置变量值。从那里提到的测试和其他测试中,我猜想在第一次运行CMakeCache.txt时是在执行 command 时创建的project

有人可以确认/纠正,引用有信誉的消息来源吗?

4

1 回答 1

1

我猜想在执行命令项目时会在第一次运行时创建 CMakeCache.txt。

它不是。


CMake 缓存(如果存在)在程序启动时被读取。否则它只初始化以下变量:

  • CMake 可执行文件:CMAKE_COMMAND, CMAKE_CPACK_COMMAND,CMAKE_CTEST_COMMAND
  • CMake 生成器信息:CMAKE_EXTRA_GENERATOR, CMAKE_GENERATOR, CMAKE_GENERATOR_INSTANCE, CMAKE_GENERATOR_PLATFORM,CMAKE_GENERATOR_TOOLSET
  • CMake 安装信息:CMAKE_HOME_DIRECTORY,CMAKE_ROOT

看这个测试:

$ cat CMakeLists.txt
get_property(vars DIRECTORY PROPERTY CACHE_VARIABLES)
message(STATUS "${vars}")

cmake_minimum_required(VERSION 3.22)
project(test NONE)
$ rm -rf build && cmake -S . -B build
-- CMAKE_COMMAND;CMAKE_CPACK_COMMAND;CMAKE_CTEST_COMMAND;CMAKE_EXTRA_GENERATOR;CMAKE_GENERATOR;CMAKE_GENERATOR_INSTANCE;CMAKE_GENERATOR_PLATFORM;CMAKE_GENERATOR_TOOLSET;CMAKE_HOME_DIRECTORY;CMAKE_ROOT
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build

project()命令自然会创建许多与语言和项目元数据相关的缓存条目。至少,在未启用任何语言的情况下,它会设置以下变量:

  • 平台信息:CMAKE_PLATFORM_INFO_INITIALIZED,,,CMAKE_MAKE_PROGRAMCMAKE_UNAME
  • 发电机设置:CMAKE_VERBOSE_MAKEFILE, CMAKE_COLOR_MAKEFILE,CMAKE_EXPORT_COMPILE_COMMANDS
  • 安装设置:CMAKE_INSTALL_PREFIX, CMAKE_INSTALL_SO_NO_EXE, CMAKE_SKIP_INSTALL_RPATH,CMAKE_SKIP_RPATH
  • 项目元数据:CMAKE_PROJECT_DESCRIPTION, CMAKE_PROJECT_HOMEPAGE_URL,CMAKE_PROJECT_NAME
  • 项目相关路径:${PROJECT_NAME}_BINARY_DIR, ${PROJECT_NAME}_IS_TOP_LEVEL,${PROJECT_NAME}_SOURCE_DIR

但是,它不会将它们写入磁盘。只有当程序正常退出时,缓存的内容才会写入磁盘。看这个测试:

$ cat CMakeLists.txt 
cmake_minimum_required(VERSION 3.22)
project(test NONE)

get_property(vars DIRECTORY PROPERTY CACHE_VARIABLES)
message(STATUS "${vars}")

execute_process(COMMAND pkill cmake)

$ rm -rf build && cmake -S . -B build
-- CMAKE_COLOR_MAKEFILE;CMAKE_COMMAND;CMAKE_CPACK_COMMAND;CMAKE_CTEST_COMMAND;CMAKE_EXPORT_COMPILE_COMMANDS;CMAKE_EXTRA_GENERATOR;CMAKE_GENERATOR;CMAKE_GENERATOR_INSTANCE;CMAKE_GENERATOR_PLATFORM;CMAKE_GENERATOR_TOOLSET;CMAKE_HOME_DIRECTORY;CMAKE_INSTALL_PREFIX;CMAKE_INSTALL_SO_NO_EXE;CMAKE_MAKE_PROGRAM;CMAKE_PLATFORM_INFO_INITIALIZED;CMAKE_PROJECT_DESCRIPTION;CMAKE_PROJECT_HOMEPAGE_URL;CMAKE_PROJECT_NAME;CMAKE_ROOT;CMAKE_SKIP_INSTALL_RPATH;CMAKE_SKIP_RPATH;CMAKE_UNAME;CMAKE_VERBOSE_MAKEFILE;test_BINARY_DIR;test_IS_TOP_LEVEL;test_SOURCE_DIR
Terminated
$ stat build/CMakeCache.txt
stat: cannot stat 'build/CMakeCache.txt': No such file or directory

请注意,将pkill cmake命令替换为message(FATAL_ERROR) 确实会保留缓存。因此,缓存通常驻留在内存中。您不需要关心缓存的确切保存时间。

于 2022-02-15T09:32:56.177 回答