使用CMake制作后如何将目录的内容复制到构建目录中? [英] How to copy contents of a directory into build directory after make with CMake?

查看:101
本文介绍了使用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
  • -E makes CMake run commands instead of building
  • copy_directory is a Command-Line Tool
  • config 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 the add_custom_command documentation.

这篇关于使用CMake制作后如何将目录的内容复制到构建目录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆