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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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 Tool$<TARGET_FILE_DIR:MyTarget>
is a generator expression, described in theadd_custom_command
documentation.
回答2:
CMake supports a shell type file copy. This link should be helpful for you - How to copy directory from source tree to binary tree?
回答3:
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".