栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

2022-01-23 Qt 编译时所依赖文件自动拷贝方案

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022-01-23 Qt 编译时所依赖文件自动拷贝方案

文章目录

前言一、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

三、使用cmake语法

目前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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/715080.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号