Copy file from source directory to binary directory using CMake

cmake copy directory
cmake copy all files in directory
cmake copy file after build
cmake copy multiple files
cmake file
cmake configure file
cmake copy header files
cmake build directory

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.

file, (If the file is a build input, use the configure_file() command to update the file data to be converted to a hexadecimal representation (useful for binary data). The COPY signature copies files, directories, and symlinks to a destination folder. add_custom_command(TARGET MyTarget PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/config $<TARGET_FILE_DIR:MyTarget>) This executes every time you build MyTarget and copies the contents of “/config” into the directory where the target exe/lib will end up.


both option are valid and targeting two different steps of your build:

  1. file(COPY ... copies the file in configuration step and only in this step. When you rebuild your project without having changed your cmake configuration, this command won't be executed.
  2. add_custom_command is the preferred choice when you want to copy the file around on each build step.

The right version for your task would be:

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

you can choose between PRE_BUILD, PRE_LINK, POST_BUILD best is you read the documentation of add_custom_command

an example on how to use the first version can be found here: Use CMake add_custom_command to generate source for another target

file, Any directories in the path specified by <filename> that do not exist will be created. If the file is a build input, use the configure_file() command to update the file data to be converted to a hexadecimal representation (useful for binary data). The COPY signature copies files, directories, and symlinks to a destination folder. 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


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

First, you forgot to close the parenthesis.

Second, the DESTINATION should be a directory, not a file name. Assuming that you closed the parenthesis, the file would end up in a folder called input.txt.

To make it work, just change it to

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

[CMake] Copying data files from source tree to binary tree during the , [CMake] Copying data files from source tree to binary tree during the build run command in a given directory copy file destination - copy file to Or you can use CONFIGURE_FILE with COPYONLY to copy files at CMake  The input path must be a file, not a directory. <output> Path to the output file or directory. A relative path is treated with respect to the value of CMAKE_CURRENT_BINARY_DIR. If the path names an existing directory the output file is placed in that directory with the same file name as the input file. COPYONLY Copy the file without replacing


I would suggest TARGET_FILE_DIR if you want the file to be copied to the same folder as your .exe file.

$ 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}>)

In VS, this cmake script will copy input.txt to the same file as your final exe, no matter it's debug or release.

Target for copying files and directories · UCL/GreatCMakeCookOff , Bunch of CMake pain in the baker. Contribute to UCL/GreatCMakeCookOff development by creating an account on GitHub. add_custom_command(TARGET MyTarget PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/config/ $<TARGET_FILE_DIR:MyTarget>) This executes every time you build MyTarget and copies the contents of "/config" into the directory where the target exe/lib will end up.


if you want to copy folder from currant directory to binary (build folder) folder

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

then the syntexe is :

file(COPY pathSource DESTINATION pathDistination)

CMake, and how to copy resources during build., So to work with a shader became quite cumbersome. Instead if there were a new file in my resource folder, it would be copied to the  The problem is that at the first build the "bin" folder doesn't exist (the copy fail), but the "bin" folder is created just after. So at the second build the copy works. Is there a way to make the custom command create the bin folder and then copy the binary ? Or is it possible to have two EXECUTABLE_OUTPUT_PATH with cmake ? Thanks !


cmake-commands, A target created in the same directory (CMakeLists.txt file) that PRE_LINK Run after sources have been compiled but before linking the binary or running Copies an <input> file to an <output> file and substitutes variable  file(COPY src/benchmarking/bagfiles DESTINATION ${CMAKE_BINARY_DIR}) Unfortunately the variable CMAKE_BINARY_DIR only contains the absolute path to my source directory. Is there a variable that points to the binaries directory?


C, cd build; cmake .. make, generate executable file "Hello" in build/src. Example 2. // util.h; #ifndef copy readme.md and license to the install directory; file(COPY  INPUT <input-file> Use the content from a given file as input. A relative path is treated with respect to the value of CMAKE_CURRENT_SOURCE_DIR. See policy CMP0070. OUTPUT <output-file> Specify the output file name to generate. Use generator expressions such as $<CONFIG> to specify a configuration-specific output file name. Multiple


CMake by Example, add_executable defines our binary with all linked source files. CMake will build the library as libtest.a and install it into lib folder of the install directory. We also  CMake is a cross-platform build system generator. Projects specify their build process with platform-independent CMake listfiles included in each directory of a source tree with the name CMakeLists.txt. Users build a project by using CMake to generate a build system for a native tool on their platform.