gpt4 book ai didi

c++ - CMake:将文件从构建目录复制到源目录

转载 作者:搜寻专家 更新时间:2023-10-31 02:10:19 25 4
gpt4 key购买 nike

我的 C++ 程序读取和写入文件。要将输入文件复制到需要的源目录到构建目录,我在 CMakeLists.txt 中使用了这一行:

configure_file(<input> <output> COPYONLY)

作为一个积极的副作用,QtCreator 在其项目 View 中列出了文件。

有没有办法自动将文件从构建目录复制到源目录,这样我就可以在源目录中获得更新的输出文件,并在 QtCreator 的项目 View 中列出?

最佳答案

如果您对此感到满意,您可能可以使用带有绝对路径的相同命令:

configure_file(${CMAKE_BINARY_DIR}/<path>/<input> ${CMAKE_SOURCE_DIR}/<path>/<output> COPYONLY)

在哪里 CMAKE_BINARY_DIR 是:

[...] the full path to the top level of the current CMake build tree.

同时 CMAKE_SOURCE_DIR 是:

[...] the full path to the top level of the current CMake source tree.

根据documentation , configure_file以自定义方式对待 <input><output>仅当它们是相对路径时,否则直接使用它们。
换句话说:

  • <input>是一个文件名:

    A relative path is treated with respect to the value of CMAKE_CURRENT_SOURCE_DIR.

    绝对路径只是按原样使用。

  • <output>是文件名或目录:

    A relative path is treated with respect to the value of CMAKE_CURRENT_BINARY_DIR.

    绝对路径只是按原样使用。


您可以使用的另一种可能方法是依赖 file 命令及其(让我说)COPY版本。根据文档:

The COPY signature copies files, directories, and symlinks to a destination folder. Relative input paths are evaluated with respect to the current source directory, and a relative destination is evaluated with respect to the current build directory. Copying preserves input file timestamps, and optimizes out a file if it exists at the destination with the same timestamp. Copying preserves input permissions unless explicit permissions or NO_SOURCE_PERMISSIONS are given (default is USE_SOURCE_PERMISSIONS).

显然绝对路径可以像configure_file一样直接使用他们保持原样。相对路径的处理方式不同,因为它通常发生在 cmake 中。 .

关于c++ - CMake:将文件从构建目录复制到源目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45476410/

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