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

查看:167
本文介绍了用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.

正如马克·拉卡塔在下面的评论中指出的那样,替换了 PRE_BUILD add_custom_command 中使用 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 是目录(位于在项目根目录下)谁的内容将被复制到构建目标中

  • $< TARGET_FILE_DIR:MyTarget> 生成器表达式,在 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天全站免登陆