CMake:将文件从源目录复制到二进制目录 [英] CMake: copy file from source directory to binary directory

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

问题描述

我试图在CLion上创建一个简单的项目。它使用CMake(我这里是新的)生成Makefiles构建项目(或某种类型)



所有我需要的是转移一些非项目文件



这个文件包含测试数据和应用程序打开它来读取它们。我尝试了几种方法:




  • Via 档案(COPY ...

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

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

    / li>
  • Via 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})



    $ b




我做错了什么?

解决方案

您可以考虑使用使用 COPYONLY 选项配置configure_file

  configure_file ; input> < output> COPYONLY)

档案(COPY ...)不同它会在输入和输出之间创建文件级依赖关系,即:


如果输入文件被修改,构建系统将重新-run CMake重新配置文件并再次生成构建系统。



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:

  • Via 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.

  • Via add_custom_command

    • OUTPUT version

      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 version

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

    But no one of it work.

What am I doing wrong?

解决方案

You may consider using configure_file with the COPYONLY option:

configure_file(<input> <output> COPYONLY)

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

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