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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:18:04  来源:igfitidea点击:

How to copy contents of a directory into build directory after make with CMake?

c++cmake

提问by Meysam

I've got some config files (xml, ini, ...) in the configdirectory 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 MyTargetand 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_BUILDwith POST_BUILDin the add_custom_commandensures that copying will only happen if the build succeeds.

正如马克Lakata指出在下面留言,替换PRE_BUILDPOST_BUILDadd_custom_command确保,如果生成成功复制才会发生。

Explanation

解释

  • ${CMAKE_COMMAND}is the path to CMake
  • -Emakes CMake run commands instead of building
  • copy_directoryis a Command-Line Tool
  • configis 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_commanddocumentation.
  • ${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”。