CMake:如何在make之后将目录的内容复制到build目录中

时间:2021-04-03 16:26:52

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目录中的所有文件复制到构建目录(可执行文件旁边)?

3 个解决方案

#1


90  

You can use add_custom_command.

您可以使用add_custom_command。

Say your target is called MyTarget, you could do:

假设您的目标名为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 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.

正如Mark Lakata在下面的评论中指出的那样,在add_custom_command中用POST_BUILD替换PRE_BUILD可确保只有在构建成功时才会进行复制。

#2


3  

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类型文件副本。此链接应该对您有所帮助 - 如何将目录从源树复制到二叉树?

#3


-4  

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”。

#1


90  

You can use add_custom_command.

您可以使用add_custom_command。

Say your target is called MyTarget, you could do:

假设您的目标名为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 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.

正如Mark Lakata在下面的评论中指出的那样,在add_custom_command中用POST_BUILD替换PRE_BUILD可确保只有在构建成功时才会进行复制。

#2


3  

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类型文件副本。此链接应该对您有所帮助 - 如何将目录从源树复制到二叉树?

#3


-4  

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”。