How to copy contents of a directory into build directory after make with CMake?(使用CMake制作后如何将目录的内容复制到构建目录中?)
问题描述
我在源文件旁边的 config 目录中有一些配置文件(xml、ini、...).每次制作项目时,如何将config目录下的所有文件都复制到build目录下(可执行文件旁边)?
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?
推荐答案
您可以使用 add_custom_command.
You can use add_custom_command.
假设您的目标名为 MyTarget,那么您可以这样做:
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>)
每次构建 MyTarget 时都会执行此操作,并将/config"的内容复制到目标 exe/lib 将结束的目录中.
This executes every time you build MyTarget and copies the contents of "/config" into the directory where the target exe/lib will end up.
正如 Mark Lakata 在下面的评论中指出的那样,将 PRE_BUILD 替换为 <add_custom_command 中的 code>POST_BUILD 确保只有在构建成功时才会进行复制.
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.
${CMAKE_COMMAND}是 CMake 的路径-E使 CMake 运行命令而不是构建copy_directory是一个 命令行工具config是目录(位于项目根目录下),其内容将被复制到构建目标中$是一个生成器表达式,在add_custom_command文档中进行了描述.
${CMAKE_COMMAND}is the path to CMake-Emakes CMake run commands instead of buildingcopy_directoryis a Command-Line Toolconfigis 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_commanddocumentation.
这篇关于使用CMake制作后如何将目录的内容复制到构建目录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用CMake制作后如何将目录的内容复制到构建目录中?
基础教程推荐
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
