I've got some config files (xml, ini, ...) in the config
directory next to the source files. How can I copy all the files in the config directory into the build directory (next to the executable file) each time I make the project?
5 Answers
You can use add_custom_command
.
Say your target is called MyTarget
, then you can do this:
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.
As Mark Lakata points out in a comment below, replacing PRE_BUILD
with POST_BUILD
in the add_custom_command
ensures that copying will only happen if the build succeeds.
Explanation
${CMAKE_COMMAND}
is the path to CMake-E
makes CMake run commands instead of buildingcopy_directory
is a Command-Line Toolconfig
is the directory (that falls under the root of the project) whose contents will be copied into the build target$<TARGET_FILE_DIR:MyTarget>
is a generator expression, described in theadd_custom_command
documentation.
-
1One of your actual targets you've got in your CMakeLists.txt - i.e something added via
add_executable
oradd_library
.– FraserNov 17, 2012 at 11:58 -
1Ok, it worked. But config files are copied only after
make clean
. A simple make will not copy the config files. Any idea?– B FaleyNov 17, 2012 at 12:03 -
7POST_BUILD is probably a better option, which means the files will only be copied if the build succeeds. Apr 20, 2015 at 17:28
-
7Is there a way to make
make clean
aware that the files copied must be deleted when the project cleans? Dec 19, 2015 at 21:54 -
3Is there a way to copy the directory itself instead of the contents?– user796530Mar 12, 2016 at 6:00
In addition to the top answer,
To copy the directory itself instead of the contents, you can add /${FOLDER_NAME}
to the end of the second parameter.
Like this:
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/config $<TARGET_FILE_DIR:${PROJECT_NAME}>/config)
CMake supports a shell type file copy. This link should be helpful for you - How to copy directory from source tree to binary tree?
Use symbolic links
CMake enables symbolic links via create_symlink
:
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
${CMAKE_SOURCE_DIR}/config $<TARGET_FILE_DIR:${PROJECT_NAME}>/config)
It ensures that when you make a change to the files in the directory, build folder would subsequently be updated.
-
1
-
3But if you are building a release, a symlink won't work as you wont have the files in place. What you are solving is that your path would be the same between debug and release, but still you would need a different command when releasing to copy the contents. Mar 30, 2022 at 8:04
In my project i use INSTALL to specify in CMake, what and where i move my binary with conf file. After execution of cmake, use "make install".
-
OP asked how to do this upon completion of build. Implication is without another command entered into the shell. Perform the build and poof the files your binary need are magically there. I too use the
install()
command, but this is for a special case: the code builds, passes unit tests, and is ready for some level of use by others. I don't install it to debug it. Nov 16, 2017 at 14:49