English 中文(简体)
来源目录使用CMake的双向名录的复印件
原标题:Copy file from source directory to binary directory using CMake
  • 时间:2016-01-14 21:17:48
  •  标签:
  • cmake

我试图就电离问题建立一个简单的项目。 它利用CMake制作成册,以建设项目(或某种类型)

我每次管理我的代码时,都需要将一些非项目档案(某种资源档案)转给双向名录。

该档案载有测试数据,可读取。 我尝试了几种方式这样做:

  • 通过<代码>(COPY......)

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

    看看好,但只是一劳永逸地工作,而不是在下一年之后再复制。

  • 通过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})
      

    但其中没有一个工作。

我做了什么错误?

最佳回答
问题回答

The first of option you tried doesn t work for two reasons.

First, you forgot to close the parenthesis.

第二,<代码>DESTINATION应为名录,而不是档案名称。 假设你关闭了括号,该档案最终将翻成一个称为input.tx的夹。

To make it work, just change it to

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

我建议TARGET_FILE_DIR。 如果你想要把档案复制到与你档案相同的文件。

$ Directory of main file (.exe, .so.1.2, .a).

add_custom_command(
  TARGET ${PROJECT_NAME} POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy 
    ${CMAKE_CURRENT_SOURCE_DIR}/input.txt 
    $<TARGET_FILE_DIR:${PROJECT_NAME}>)

在VS,这一薄膜将复制投入。 txt to the same file as You final exe, no matter it s debug or release.

The suggested configure_file is probably the easiest solution. However, it will not rerun the copy command to if you manually deleted the file from the build directory. To also handle this case, the following works for me:

add_custom_target(copy-test-makefile ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/input.txt)
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
                   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)

如果你想要复制从粗鲁的目录到双双倍(双倍)的夹

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/yourFolder/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/yourFolder/)

then the syntexe is :

file(COPY pathSource DESTINATION pathDistination)

This is what I used to copy some resource files: the copy-files is an empty target to ignore errors

 add_custom_target(copy-files ALL
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${CMAKE_BINARY_DIR}/SOURCEDIRECTORY
    ${CMAKE_BINARY_DIR}/DESTINATIONDIRECTORY
    )

如果你不希望对双亲名录中的档案进行修改,那么你就能够避免书面习惯指令的复杂性。 CMake支持建立电解链(或视窗上相当的东西)。 它有https://cmake.org/cmake/help/latest/command/file.html#create-link” rel=“nofollow noreferer”>:>>>-rellowation,以及

如果您不希望来源目录的复印件能够更改/或每改动一次重新配置人工,则您可使用 or the file(COPY [...])<>>>><<>>>>>>/>。

If you want to put the content of example into install folder after build:

code/
  src/
  example/
  CMakeLists.txt

try add the following to your CMakeLists.txt:

install(DIRECTORY example/ DESTINATION example)

Single File

  1. Add a rule for the copying. OUTPUT specifies the produced file and DEPENDS creates a dependency, so when the input changes this rule is run again.
set(MY_RESOURCE_FILE input.txt)
add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MY_RESOURCE_FILE}
        COMMAND ${CMAKE_COMMAND} -E copy
            ${CMAKE_CURRENT_SOURCE_DIR}/${MY_RESOURCE_FILE}
            ${CMAKE_CURRENT_BINARY_DIR}/${MY_RESOURCE_FILE}
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${MY_RESOURCE_FILE}
  1. Then add the result file as a dependency to the target that needs it.
add_executable(main_executable
    ${CMAKE_CURRENT_BINARY_DIR}/${MY_RESOURCE_FILE}

    src/main.cpp
)

amodern and Clean CMake解决方案,因为所有附属企业都是以目标为基础,并根据需要推广依赖树木。 我们没有改变任何全球状态。 档案本身也跟踪,因此只有在档案过期时才能复制。 这也是一个有力和有效的解决办法。

In case its needed by multiple targets, add them to all of them. This is good practice, as dependencies are explicitly specified, exactly where they are needed.

Multiple Files

As some people here asked for copying multiple files, I am adding this here as well, as a few more things need to be considered.

界定档案有两种可能性,一种可以人工列出,类似于上文。 或者,我们可以制定自动发现的<条码>glob规则。 第一种是非常直截了当的,使用相对的路向CMAKE_CURRENT_SOURCE_DIR

set(MY_RESOURCE_FILES 
    resources/font.ttf
    resources/icon.svg
)

I am also showing the second glob case, at requires a bit more consideration.

file(GLOB_RECURSE
    MY_RESOURCE_FILES
    RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} 
    CONFIGURE_DEPENDS

    ${CMAKE_CURRENT_SOURCE_DIR}/resources/**.ttf    # adapt these to your needs
    ${CMAKE_CURRENT_SOURCE_DIR}/resources/**.svg
)

Specifying CONFIGURE_DEPENDS re-runs the glob at the beginning the build when any target is out-of date. And RELATIVE gives us relative path to our source directory. And using two stars **.svg recursively searches all sub-directories as well.

现在,我们再次制定一条单独的规则,使每项资源都存档。

FOREACH(MY_RESOURCE_FILE ${MY_RESOURCE_FILES})
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MY_RESOURCE_FILE}
        COMMAND ${CMAKE_COMMAND} -E copy
            ${CMAKE_CURRENT_SOURCE_DIR}/${MY_RESOURCE_FILE}
            ${CMAKE_CURRENT_BINARY_DIR}/${MY_RESOURCE_FILE}
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${MY_RESOURCE_FILE}
  )
ENDFOREACH()

This will also make it that only the resources missing or changed are copied.

最后,我们把所有这些内容补充到我们可执行的行动中。 为此,我们刚刚需要调整这条道路,使之适应双亲。

list(TRANSFORM MY_RESOURCE_FILES PREPEND ${CMAKE_CURRENT_BINARY_DIR}/)

add_executable(${MAIN_TARGET} 
    ${MY_RESOURCE_FILES}

    src/main.cpp
)




相关问题
Why can t CLion see header files, WSL2 toolchain?

My setup: Windows 10 Pro 22H2 (19045.2965) CLion 2020.1.3 (JetBrains) WSL2 with Ubuntu 22.04.2 LTS gcc 11.3.0 cmake 3.22.1 gdb 12.1 gmake 4.3 CMakeLists.txt cmake_minimum_required(VERSION 3.22)...

CMake linking problem

I am trying to use CMake to compile a C++ application that uses the C library GStreamer. My main.cpp file looks like this: extern "C" { #include <gst/gst.h> #include <glib.h> } int main(...

How to add an extra plist property using CMake?

I m trying to add the item <key>UIStatusBarHidden</key><true/> to my plist that s auto-generated by CMake. For certain keys, it appears there are pre-defined ways to add an item; ...

(c)make - resursive compile

Let s assume I have directories like: dir1 main.cpp dir2 abc.cpp dir3 def.cpp dir4 ghi.cpp jkl.cpp And let s assume that main.cpp ...

Reading registry values with cmake

On a Windows 7 machine I cannot read any registry values that contain a semicolon. For example if you have 7-zip, running the following SET(MYPATH [HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip;Path]) ...

Finding the correct Python framework with cmake

I am using the macports version of python on a Snow Leopard computer, and using cmake to build a cross-platform extension to it. I search for the python interpreter and libraries on the system using ...

热门标签