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

查看:96
本文介绍了使用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


  • 输出版本

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)


  • 目标版本

    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 选项:

    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天全站免登陆