CMake:添加自定义资源以建立目录 [英] CMake: adding custom resources to build directory

查看:247
本文介绍了CMake:添加自定义资源以建立目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个小程序,需要一个图像文件foo.bmp来运行

,以便我可以编译该程序但要运行它,我必须手动将foo.bmp复制到 build子目录中

I am making a small program which requires an image file foo.bmp to run
so i can compile the program but to run it, i have to copy foo.bmp to 'build' subdirectory manually

我应该在CMakeLists.txt中使用什么命令在程序编译时自动添加foo.bmp来建立子目录?

what command should i use in CMakeLists.txt to automatically add foo.bmp to build subdirectory as the program compiles?

推荐答案

为此,您应该使用add_custom_command为构建目录中所需的文件生成构建规则。然后将目标中的依赖项添加到这些文件中:CMake仅在目标需要时才构建某些东西。

To do that you should use add_custom_command to generate build rules for file you needs in the build directory. Then add dependencies from your targets to those files: CMake only build something if it's needed by a target.

您还应该确保仅复制不需要的文件

You should also make sure to only copy files if you're not building from the source directory.

类似这样的东西:

project(foo)

cmake_minimum_required(VERSION 2.8)

# we don't want to copy if we're building in the source dir
if (NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)

    # list of files for which we add a copy rule
    set(data_SHADOW yourimg.png)

    foreach(item IN LISTS data_SHADOW)
        message(STATUS ${item})
        add_custom_command(
            OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${item}"
            COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/${item}" "${CMAKE_CURRENT_BINARY_DIR}/${item}"
            DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${item}"
        )
    endforeach()
endif()

# files are only copied if a target depends on them
add_custom_target(data-target ALL DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/yourimg.png")

在这种情况下,我使用依赖于yourimg.png文件的 ALL自定义目标强制执行复制,但是您也可以添加现有目标之一的依赖性。

In this case I'm using a "ALL" custom target with a dependency on the yourimg.png file to force the copy, but you can also add dependency from one of your existing targets.

这篇关于CMake:添加自定义资源以建立目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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