使用QMake语法方便复制文件或文件夹。
使用
# 配置file_copies
CONFIG += file_copies
# 创建examples变量并配置
# 配置需要复制的文件或目录(支持通配符)
examples.files = $$PWD/MyExamples
# 配置需要复制的目标目录, $$OUT_PWD为QMake内置变量,含义为程序输出目录
examples.path = $$OUT_PWD
# 配置COPIES
COPIES += examples
来源
从Qt5.6引入,该方法是非官方文档说明,后续可能不支持。
https://codereview.qt-project.org/c/qt/qtbase/+/156784
以Qt 5.12.1的MSVC版本文件路径。
C:\Qt\Qt5.12.1\5.12.1\msvc2017_64\mkspecs\features\file_copies.prf
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓
实现
file_copies.prf对应源码:
isEmpty(COPIES): return()
contains(TEMPLATE, .*subdirs): error("COPIES does not work with TEMPLATE=subdirs")
build_pass:build_all:!isEqual(BUILD_PASS, $$first(BUILDS)) {
# Avoid that multiple build passes race with each other.
# This will fail to copy anything if the user explicitly invokes
# only the non-primary build. This is unfixable, as at qmake time
# we cannot possibly know how make will be invoked, yet we must
# predict it here.
return()
}
defineReplace(qtStripProPwd) {
return($$relative_path($$1, $$_PRO_FILE_PWD_))
}
for (cp, COPIES) {
isEmpty($${cp}.files): next()
pfx = copy_$${cp}
notdir = false
dir = false
for (f, $${cp}.files) {
fil = $$absolute_path($$f, $$_PRO_FILE_PWD_)
tfiles = $$files($$fil/*)
isEmpty(tfiles): \
notdir = true
else: \
dir = true
$${pfx}.files += $$fil
}
$$dir:$$notdir: \
error("COPIES entry $$cp lists both files and directories.")
path = $$eval($${cp}.path)
isEmpty(path): error("COPIES entry $$cp defines no .path")
base = $$eval($${cp}.base)
isEmpty(base) {
$${pfx}.output = $$path/${QMAKE_FILE_IN_NAME}
} else: isEqual(base, $$_PRO_FILE_PWD_) {
$${pfx}.output = $$path/${QMAKE_FUNC_FILE_IN_qtStripProPwd}
} else {
eval(defineReplace(qtStripSrcDir_$$cp) { \
return(\$\$relative_path(\$\$1, $$val_escape(base))) \
})
$${pfx}.output = $$path/${QMAKE_FUNC_FILE_IN_qtStripSrcDir_$$cp}
}
$${pfx}.input = $${pfx}.files
$${pfx}.commands = $(QINSTALL) ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT}
$${pfx}.name = COPY ${QMAKE_FILE_IN}
$${pfx}.CONFIG = no_link no_clean target_predeps
QMAKE_EXTRA_COMPILERS += $${pfx}
}
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓