前言一、QMAKE_POST_link二、利用qtcreatordata.pri三、使用cmake语法总结
前言
在qt 程序开发的时候,经常会需要在编译的同时将非Qt库放至输出目录,以减少手动复制文件的操作
一、QMAKE_POST_link使用 QMAKE_POST_link 进行复制
QMAKE_POST_link += $$escape_expand(n) $$QMAKE_COPY "SRC_DIR" "DST_DIR"
使用放入pro文件或者pri文件中就可以了,但这个在windows和linux下使用会有所不同,在windows下目录必须要使用\
二、利用qtcreatordata.pri需要写一个pro文件
copy.pro
# 创建一个不生成任何内容的Makefile。 如果不需要调用任何编译器来创建目标,请使用此选项;
TEMPLATE = aux
# STATIC_FILES中列出的文件的基础目录
STATIC_base = $$PWD
# 编译输出的基础目录
STATIC_OUTPUT_base = $$OUT_PWDbin
# 安装输出的基础目录
STATIC_INSTALL_base = $$OUT_PWDbin
# 要部署的文件列表
STATIC_FILES =
$$PWD/1.txt
$$PWD/2.txt
include(qtcreatordata.pri)
qtcreatordata.pri
# This pri file is used to deploy files that are not compiled while building
# Qt Creator. It handles copying of files into the build directory if using
# a shadow build and adds the respective install target as well.
#
# Usage: Define variables (details below) and include this pri file afterwards.
#
# STATIC_base - base directory for the files listed in STATIC_FILES
# STATIC_FILES - list of files to be deployed
# STATIC_OUTPUT_base - base directory in the compile output
# STATIC_INSTALL_base - base directory in the install output
# used in custom compilers which just copy files
defineReplace(stripStaticbase) {
return($$relative_path($$1, $$STATIC_base))
}
# handle conditional copying based on STATIC_base compared to STATIC_OUTPUT_base
!isEmpty(STATIC_FILES) {
isEmpty(STATIC_base):
error("Using STATIC_FILES without having STATIC_base set")
isEmpty(STATIC_OUTPUT_base):
error("Using STATIC_FILES without having STATIC_OUTPUT_base set")
!osx:isEmpty(STATIC_INSTALL_base):
error("Using STATIC_FILES without having STATIC_INSTALL_base set")
!isEqual(STATIC_base, $$STATIC_OUTPUT_base) {
copy2build.input += STATIC_FILES
copy2build.output = $$STATIC_OUTPUT_base/${QMAKE_FUNC_FILE_IN_stripStaticbase}
isEmpty(vcproj):copy2build.variable_out = PRE_TARGETDEPS
win32:copy2build.commands = $$QMAKE_COPY "${QMAKE_FILE_IN}" "${QMAKE_FILE_OUT}"
unix:copy2build.commands = $$QMAKE_COPY ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT}
copy2build.name = COPY ${QMAKE_FILE_IN}
copy2build.ConFIG += no_link no_clean
QMAKE_EXTRA_COMPILERS += copy2build
for(static_file, STATIC_FILES) {
QMAKE_DISTCLEAN += $$STATIC_OUTPUT_base/$$stripStaticbase($$static_file)
}
}
static.files = $$STATIC_FILES
static.base = $$STATIC_base
static.path = $$STATIC_INSTALL_base
INSTALLS += static
}
然后将copy.pro 包含到项目中即可,,注意设置cpoy.pro 中的路径
此方法来自qtcreator 中,有兴趣的小伙伴可以看到去,注意QtCreator 6.0 以后全部使用cmake管理工程了,所以如果要查看qmake的方案,需看QtCreator 6.0 以前版本的源码https://github.com/qt-creator/qt-creator/tree/v5.0.3/src/share/qtcreator
目前Qt官方从Qt6.0 开始全部使用cmake开始管理项目,整体趋势也是面向cmake
https://github.com/qt-creator/qt-creator/blob/v6.0.2/src/share/qtcreator/externaltools/CMakeLists.txt
可以参考CMakeLists.txt 实现的和qtcreatordata.pri 一样
有空多看看源码是极好的,另外这个博客写的很全面,推荐看看https://www.cnblogs.com/codeForFamily/p/qt-creator-ide-source-learn-3-3.html



