gpt4 book ai didi

cmake - 当依赖文件被修改时,如何让 CMake 重新运行 add_custom_command?

转载 作者:行者123 更新时间:2023-12-05 02:54:05 25 4
gpt4 key购买 nike

每次修改我提供的列表中的文件时,我都希望重新运行我的自定义命令。

我的例子:我的项目有以下文件:

  • main.cpp
  • CMakeLists.txt
  • dep1.txt
  • dep2.txt
cmake_minimum_required(VERSION 3.17)

project(dummy)
set(DummyFiles dep1.txt, dep2.txt)

add_executable(test_dummy main.cpp)

add_custom_command(TARGET test_dummy
COMMENT "ran custom command on file change"
DEPENDS ${DummyFiles}
)

我的期望是,之后我已经配置了那个项目,每次我修改 dep1.txt 或 dep2.txt 并重新配置时,CMake 都会打印出 COMMENT 部分以上。然而,它没有。

如有任何帮助,我们将不胜感激。

最佳答案

命令有两个流程 add_custom_command : “生成文件”和“构建事件”。

选项 DEPENDS 仅适用于第一个流程 - “生成文件”,它需要 OUTPUT 作为第一个选项。

您使用 TARGET 作为命令的第一个选项,表示“构建事件”命令流。此命令流不支持 DEPENDS 选项(此命令流的概要中没有此类选项)。

I want to have my custom commands rerun every time a file from a list I supply gets modified.

为此,您需要使用 add_custom_commandfirst flowOUTPUT 选项。

您可以使用虚拟文件作为输出,因此构建系统可以将此文件的时间戳与 DEPENDS 部分中文件的时间戳进行比较。每当发现 OUTPUT 的时间戳早于 DEPENDS 之一的时间戳时,该命令将重新运行。

set(DummyFiles dep1.txt dep2.txt)

add_custom_command(OUTPUT dummy.txt
COMMENT "ran custom command on file change"
DEPENDS ${DummyFiles}
)

# Need to create a custom target for custom command to work
add_custom_target(my_target ALL
# Use absolute path for the DEPENDS file.
# Relative paths are interpreted relative to the source directory.
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/dummy.txt
)

关于cmake - 当依赖文件被修改时,如何让 CMake 重新运行 add_custom_command?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61947527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com