cmake不会在自定义命令中复制文件 [英] cmake does not copy file in custom command

查看:1046
本文介绍了cmake不会在自定义命令中复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将qml文件从源目录复制到构建目录。以下脚本只工作第一次。当我更改任何* .qml文件并运行make时,它们不会复制到构建文件夹,它们不会更新。我做错了什么?

I want to copy qml files from source directory to build directory. Following script works fine only first time. When I change any of the *.qml files and run make, they are not copied to build folder, they are not updated. What am I doing wrong?

file(GLOB_RECURSE SOURCES *.cpp)
file(GLOB_RECURSE QMLS *.qml)

add_library(MyLib SHARED ${SOURCES} ${QMLS})

foreach(QmlFile ${QMLS})
  add_custom_command(TARGET MyLib POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${QmlFile} $<TARGET_FILE_DIR:MyLib>)
endforeach()


推荐答案

虽然Angew的回答很好,可以消除对额外目标的使用。为此, add_library 调用应使用复制的 .qml >原始,如同问题文章中的脚本):

While Angew's answer is good, it is possible to eliminate usage of additional target. For doing this, add_library call should use copied .qml files (instead of original ones, like in the script in the question post):

# This part is same as in Angew's answer
set(copiedQmls "")
foreach(QmlFile ${QMLS})
  get_filename_component(nam ${QmlFile} NAME)
  add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${nam}
    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${QmlFile} ${CMAKE_CURRENT_BINARY_DIR}
    DEPENDS ${QmlFile}
    COMMENT "Copying ${QmlFile}"
    VERBATIM
  )
  list(APPEND copiedQmls ${CMAKE_CURRENT_BINARY_DIR}/${nam})
endforeach()

# But instead of creating new target, we reuse library one.
add_library(MyLib SHARED ${SOURCES} ${copiedQmls})

> target 时,它会触发 add_library 调用中的非来源文件(如果有对应的 add_custom_command 调用),但更新非源文件不会强制重建库 。这是为什么您的原始代码无法正常工作的原因。

When library target is built, it triggers non-sources files in add_library call to be updated (if there is corresponded add_custom_command call), but updating non-source files doesn't force library file to be rebuilt. This is why your original code doesn't work as expected.

请注意,因为 .qml 作为来源,您不需要为他们设置 GENERATED 属性,如这里

Note, because .qml files are not recognized by CMake as sources, you doesn't need to set GENERATED property for them, as stated here.

这篇关于cmake不会在自定义命令中复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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