C++ 使用CMake制作后如何将目录的内容复制到构建目录中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13429656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to copy contents of a directory into build directory after make with CMake?
提问by Meysam
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?
我config
在源文件旁边的目录中有一些配置文件(xml、ini、...)。每次创建项目时,如何将 config 目录中的所有文件复制到 build 目录(可执行文件旁边)?
回答by Fraser
You can use add_custom_command
.
您可以使用add_custom_command
.
Say your target is called MyTarget
, then you can do this:
假设你的目标被调用MyTarget
,那么你可以这样做:
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.
每次构建时都会执行此操作,MyTarget
并将“/config”的内容复制到目标 exe/lib 最终所在的目录中。
As Mark Lakatapoints 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.
正如马克Lakata指出在下面留言,替换PRE_BUILD
用POST_BUILD
的add_custom_command
确保,如果生成成功复制才会发生。
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) who's contents will be copied into the build target$<TARGET_FILE_DIR:MyTarget>
is a generator expression, described in theadd_custom_command
documentation.
${CMAKE_COMMAND}
是 CMake 的路径-E
使 CMake 运行命令而不是构建copy_directory
是一个命令行工具config
是内容将被复制到构建目标的目录(位于项目的根目录下)$<TARGET_FILE_DIR:MyTarget>
是文档中描述的生成器表达式add_custom_command
。
回答by hyrchao
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.
要复制目录本身而不是内容,您可以添加/${FOLDER_NAME}
到第二个参数的末尾。
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)
回答by rajneesh
CMake supports a shell type file copy. This link should be helpful for you - How to copy directory from source tree to binary tree?
CMake 支持 shell 类型的文件复制。此链接应该对您有所帮助 -如何将目录从源树复制到二叉树?
回答by Luigi Menale
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".
在我的项目中,我使用 INSTALL 在 CMake 中指定使用 conf 文件移动我的二进制文件的内容和位置。执行 cmake 后,使用“make install”。