使用 CMake 将文件从源目录复制到二进制目录 [英] Copy file from source directory to binary directory using CMake

查看:18
本文介绍了使用 CMake 将文件从源目录复制到二进制目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 CLion 上创建一个简单的项目.它使用 CMake(我是新来的)来生成 Makefile 来构建项目(或某种项目)

I'm trying to create a simple project on CLion. It uses CMake (I'm new here) to generate Makefiles to build project (or some sort of it)

每次运行代码时,我只需要将一些非项目文件(某种资源文件)传输到二进制目录.

All I need to is transfer some non-project file (some sort of resource file) to binary directory each time when I run the my code.

该文件包含测试数据,应用程序打开它以读取它们.我尝试了几种方法:

That file contains test data and application open it to read them. I tried several ways to do so:

  • 通过 file(COPY ...

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
        DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/input.txt

看起来不错,但它只工作一次,下次运行后不会重新复制文件.

Looking good but it work just once and not recopy file after next run.

通过add_custom_command

  • OUTPUT 版本

add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/input.txt
        COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
                ${CMAKE_CURRENT_BINARY_DIR}/input.txt)

  • TARGET 版本

    add_custom_target(foo)
    add_custom_command(
            TARGET foo
            COMMAND ${CMAKE_COMMAND} copy
                    ${CMAKE_CURRENT_BINARY_DIR}/test/input.txt
                    ${CMAKE_SOURCE_DIR})
    

  • 但没有一个有效.

    我做错了什么?

    推荐答案

    您可以考虑使用 configure_file 带有 COPYONLY 选项:

    You may consider using configure_file with the COPYONLY option:

    configure_file(<input> <output> COPYONLY)
    

    file(COPY ...) 不同的是,它在输入和输出之间创建了文件级的依赖关系,即:

    Unlike file(COPY ...) it creates a file-level dependency between input and output, that is:

    如果输入文件被修改,构建系统将重新运行 CMake 以重新配置文件并再次生成构建系统.

    If the input file is modified the build system will re-run CMake to re-configure the file and generate the build system again.

    这篇关于使用 CMake 将文件从源目录复制到二进制目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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