• QT判断平台和生成版本设置输入目录



    在这里插入图片描述
    个人写的一些简单工程配置项目

    #//统一Windows平台的方法
    win32 {
    
    CONFIG(debug,debug|release){ #debug
    build_type = bin_mingw32_debug
    } else { #release
    build_type = bin_mingw32_release
    }
    
    MOC_DIR = $$PWD/../../$$build_type/moc
    RCC_DIR = $$PWD/../../$$build_type/rcc
    UI_DIR = $$PWD/../../$$build_type/ui
    OBJECTS_DIR = $$PWD/../../$$build_type/objs
    DESTDIR = $$PWD/../../$$build_type
    
    }
    
    #//仅msvc编译器
    win32-msvc*{
    
    CONFIG(debug,debug|release){ #debug
    
    build_type = bin_msvc32_debug
    } else {
    build_type = bin_msvc32_release
    }
    
    MOC_DIR = $$PWD/../../$$build_type/moc
    RCC_DIR = $$PWD/../../$$build_type/rcc
    UI_DIR = $$PWD/../../$$build_type/ui
    OBJECTS_DIR = $$PWD/../../$$build_type/objs
    DESTDIR = $$PWD/../../$$build_type
    
    }
    
    # 指的是当前正在解析的.pro文件的目录的完整路径。
    # 在编写支持影子构建的项目文件时,PWD很有用。
    message(PWD is: $$PWD)
    # 指的是qmake生成的Makefile的目录的完整路径,即构建目录。
    # 例如build-??-Desktop_Qt_5_12_8_MSVC2017_64bit-Debug
    message(OUT_PWD is: $$OUT_PWD)
    # 正在使用的项目文件的路径
    message(_PRO_FILE_ is: $$_PRO_FILE_)
    # 包含目录的路径,该目录包含正在使用的项目文件
    message(_PRO_FILE_PWD_ is: $$_PRO_FILE_PWD_)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    pro工程文件中常用的宏定义

    TEMPLATE = app
    
    HEADERS:需要包含的头文件的列表。
    
    SOURCES:需要的源文件的列表。
    
    FORMS:需要的.ui文件的列表。
    
    LEXSOURCES:所有lex源文件的列表。
    
    YACCSOURCES:所有yacc源文件的列表。
    
    TARGET:可执行应用程序的名称。默认值为项目文件的名字。
    
    DESTDIR:放置可执行程序目标的目录。
    
    OBJECTS_DIR:放置obj中间文件的目录。
    
    MOC_DIR: moc转换文件路径。
    
    RCC_DIR: 资源文件路径。
    
    UI_DIR:ui文件转换的路径。
    
    RESOURCES:需要包含的资源文件。
    
    LIBS:依赖库的路径和名称 -L{xxdirxx} -l{xxnamexx}。
    
    LIBEXT: 产生lib的后缀。
    
    DEFINES:应用程序所需的额外的宏定义列表。
    
    INCLUDEPATH:应用程序所需的额外的包含路径列表。
    
    DEPENDPATH:应用程序所依赖的搜索路径。
    
    VPATH:寻找补充文件的搜索路径。
    
    DEF_FILE:只有Windows需要:应用程序所要连接的.def文件。
    
    RC_FILE:只有Windows需要:应用程序的资源文件。
    
    RES_FILE:只有Windows需要:应用程序所要连接的资源文件。
    
    TRANSLATIONS: 多国语言支持文件。
    
    INSTALLS: 要安装的文件。
    
    target.path: 安装的路径。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    Chapter1 QT判断平台和生成版本设置输入目录

    在pro文件添加

    greaterThan(QT_MAJOR_VERSION,4){
            TARGET_ARCH=$${QT_ARCH}
    }else{
            TARGET_ARCH=$${QMAKE_HOST.arch}
    }
    contains(TARGET_ARCH, x86_64){
        CONFIG(debug,debug|release){
            MOC_DIR = build/x64/tmp_debug/moc
            RCC_DIR = build/x64/tmp_debug/rcc
            UI_DIR = build/x64/tmp_debug/ui
            OBJECTS_DIR = build/x64/tmp_debug/obj
            DESTDIR = build/x64/bin/debug/
        }else{
            MOC_DIR = build/x64/tmp_release/moc
            RCC_DIR = build/x64/tmp_release/rcc
            UI_DIR = build/x64/tmp_release/ui
            OBJECTS_DIR = build/x64/tmp_release/obj
            DESTDIR = build/x64/bin/release/
        }
    }else{
        CONFIG(debug,debug|release){
            MOC_DIR = build/x86/tmp_debug/moc
            RCC_DIR = build/x86/tmp_debug/rcc
            UI_DIR = build/x86/tmp_debug/ui
            OBJECTS_DIR = build/x86/tmp_debug/obj
            DESTDIR = build/x86/bin/debug/
        }else{
            MOC_DIR = build/x86/tmp_release/moc
            RCC_DIR = build/x86/tmp_release/rcc
            UI_DIR = build/x86/tmp_release/ui
            OBJECTS_DIR = build/x86/tmp_release/obj
            DESTDIR = build/x86/bin/release/
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    Chapter2 Qt pro文件中判断 x86/arm(aarch64)交叉编译环境,区分 linux/windows系统, debug/release版本

    原文链接

    TARGET:这个配置项用来指定最后生成的目标应用程序的名称。
    
    DESTDIR:设置目标文件的输出目录(如exe或lib文件)。
    
    OBJECTS_DIR:放置obj中间文件的目录。
    
    MOC_DIR: moc转换文件的路径。
    
    RCC_DIR: 资源文件的路径。
    
    UI_DIR:ui文件转换的路径。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    使用的arm编译器为aarch64, 很多博客都是使用 QMAKE_HOST.arch 进行判断,但这并不能分辨出 aarch64 编译器,有的更离谱的是定义一个函数,实在让人无言,自行尝试解决方案如下:

    contains(QT_ARCH, arm64){
    message("arm64") #在这里处理arm64所需
    }else{
    message("x86")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    linux/windows 区分:

    win32{
    //do something...
    }
    unix{
    //do something...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    debug / release 区分:

    CONFIG += debug_and_release
    CONFIG(debug, debug|release){  //处理debug
    }else{  //处理release
    }
    
    • 1
    • 2
    • 3
    • 4

    都可以组合使用,如:

    CONFIG += debug_and_release
    CONFIG(debug, debug|release){  //处理debug
        win32{
        }
        unix{
        	contains(QT_ARCH, arm64){
    			message("arm64")
    			}else{
    			message("x86")
    			}
        }
    }else{     //处理release
        win32{
        }
        unix{
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    若根据某些宏是否存在来执行不同的配置则可以:

    添加宏定义:

    DEFINES += TEST

    判断宏定义是否存在

    contains(DEFINES, TEST) {
        message('-->>(DEFINES, TEST)')
    } else {
        message('-->>not contains (DEFINES, TEST)')
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Chapter3 Qt的版本判断、跨平台选择与pro工程文件输出设定($$$)

    原文链接:https://blog.csdn.net/u013441358/article/details/123843735

    跨平台代码
    在Pro文件中区分
    在工程文件中,使用unix、win32、macx等标号区分不同的平台。可以使用标号+冒号的形式指定单行内容在特定平台生效,也可以使用大括号选择多行内容在特定平台生效。注意使用多行选择时,左大括号和平台名需要在同一行中,否则会失效。

    windows平台多行写法示例:

    //统一Windows平台的方法
    win32 {
      
    }
     
    //仅msvc编译器
    win32-msvc*{
     
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Linux或unix平台多行写法示例:

    统一Linux或unix平台写法
    unix {
     
    }
     
    //仅g++编译器
    linux-g++*{
     
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    macOS平台多行写法示例:

    macx {
        
    }
    
    macx:qesp_mac_framework {
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    需要特别注意的是,macOS平台也是unix平台的一种,因此上述Linux或unix平台写法也包含了macOS系统。如果要指定非macOS系统的Unix平台,那么需要使用如下写法:

    unix:!macx {
    
    }
    
    
    • 1
    • 2
    • 3
    • 4

    在代码中区分
    使用宏定义选择不同平台进行条件编译。不同平台的宏定义使用示例如下:

    #if defined Q_OS_DARWIN     //Q_OS_MAC
        qputenv("QT_MAC_WANTS_LAYER", "1");
    #elif defined Q_OS_LINUX    //Q_OS_LINUX
    
    #elif defined Q_OS_WIN32    // Q_OS_WIN32
    
    #else
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    pro工程输出
    Release与Debug控制
    Release编译与Debug编译的选择同样分为单行指定和多行指定。单行指定的示例如下:

    CONFIG(debug, debug|release):LIBS += -L../lib1 -lhellod
    CONFIG(release, debug|release):LIBS += -L../lib2 -lhello
    
    • 1
    • 2

    多行指定的示例如下:

    CONFIG(debug, debug|release){
        
    } else {
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    之所以采用CONFIG(debug, debug|release)这样的写法,主要是因为要确保只能有一个条件处于active的状态。两个参数,前者是要判断的active的选项,后者是互斥的选项的一个集合。

    Qt Creator的构建配置中除了Debug与Release之后,还有Profile。Qt在qmake项目.pro文件时,会将Profile配置视作Release配置解析。

    编译生成文件

    TARGET:这个配置项用来指定最后生成的目标应用程序的名称。

    DESTDIR:设置目标文件的输出目录(如exe或lib文件)。

    OBJECTS_DIR:放置obj中间文件的目录。

    MOC_DIR: moc转换文件的路径。

    RCC_DIR: 资源文件的路径。

    UI_DIR:ui文件转换的路径。

    使用示例1:

    win32:CONFIG(release, debug|release):{
        DESTDIR = $$PWD/release
        UI_DIR = $$PWD/tmp/release/ui
        MOC_DIR = $$PWD/tmp/release/moc
        OBJECTS_DIR = $$PWD/tmp/release/obj
        RCC_DIR = $$PWD/tmp/release/rcc
    }
    else:win32:CONFIG(debug, debug|release):{
        DESTDIR = $$PWD/debug
        UI_DIR = $$PWD/tmp/debug/ui
        MOC_DIR = $$PWD/tmp/debug/moc
        OBJECTS_DIR = $$PWD/tmp/debug/obj
        RCC_DIR = $$PWD/tmp/debug/rcc
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    使用示例2:

    CONFIG(debug, debug|release) {
        build_type = debug
    } else {
        build_type = release
    }
     
    DESTDIR     = $$build_type/out
    OBJECTS_DIR = $$build_type/obj
    MOC_DIR     = $$build_type/moc
    RCC_DIR     = $$build_type/rcc
    UI_DIR      = $$build_type/ui
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    执行系统命令
    使用**QMAKE_PRE_LINK和QMAKE_POST_LINK定义需要在编译前与编译后需要执行的系统命令。一般可以利用此功能将一些依赖的头文件、库文件等复制到指定的目录中。**执行多条系统命令时,从第二条命令开始要加上“&&”作为分隔,否则会导致命令执行失败。示例如下:

    win32 {
        QT_PROJ_ROOTDIR = E:/Working/QtDev
    
        # Copy the interface file of the plugin to the folder
        InterfaceFile = $$PWD/ipluginmanager.h
        # 将LibFile中的"/"替换为"\"
        InterfaceFile = $$replace(InterfaceFile, /, \\)
    
        # Copy the library of the plugin to the folder
        LibraryFile = $$DESTDIR/EWhalesPluginManager.dll
        # 将LibFile中的"/"替换为"\"
        LibraryFile = $$replace(LibraryFile, /, \\)
    
        # 输出目录也是一样,要将"/"替换为"\"
        OutLibFile = $$QT_PROJ_ROOTDIR/Plugins/
        OutLibFile = $$replace(OutLibFile, /, \\)
    
        QMAKE_POST_LINK += "copy $$InterfaceFile $$OutLibFile"
        QMAKE_POST_LINK += "&& copy $$LibraryFile $$OutLibFile"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    完整示例
    在实际的项目工程文件中,会同时使用操作系统选择、构建类型选择、指定输出目录、编译后命令执行等功能。为了规范多个项目的编译输出框架,可以使用子项目包含的形式。在子项目中制定好编译输出的一般规则,并使多个工程文件都包含同一个子项目(.pri文件)。以下提供一个来源于真实项目的完整示例。

    通用子项目文件

    CONFIG(debug, debug|release) {
        build_type = debug
    } else {
        build_type = release
    }
    
    DESTDIR     = ../output/$$build_type
    OBJECTS_DIR = ./$$build_type/obj
    MOC_DIR     = ./$$build_type/moc
    RCC_DIR     = ./$$build_type/rcc
    UI_DIR      = ./$$build_type/ui
    
    # 指的是当前正在解析的.pro文件的目录的完整路径。 
    # 在编写支持影子构建的项目文件时,PWD很有用。
    message(PWD is: $$PWD)
    # 指的是qmake生成的Makefile的目录的完整路径,即构建目录。
    # 例如build-??-Desktop_Qt_5_12_8_MSVC2017_64bit-Debug
    message(OUT_PWD is: $$OUT_PWD)
    # 正在使用的项目文件的路径
    message(_PRO_FILE_ is: $$_PRO_FILE_)
    # 包含目录的路径,该目录包含正在使用的项目文件
    message(_PRO_FILE_PWD_ is: $$_PRO_FILE_PWD_)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    具体工程文件

    include(./ewhales_qt.pri)
    
    QT       += core gui network
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    unix:QMAKE_CXXFLAGS += -std=c++11
    TARGET = EWhalesPluginManager
    TEMPLATE = lib
    #TEMPLATE = app
    
    CONFIG             += plugin
    
    SOURCES += main.cpp\
        pluginmanager.cpp \
        pluginmanager.settings.cpp \
        pluginmanager.updater.cpp \
        pluginmanager_panel.cpp \
        pluginmanager_panel.uislots.cpp
    
    HEADERS  += \
        ipluginmanager.h \
        pluginmanager.h
    
    FORMS    += \
        pluginmanager_panel.ui
    
    INCLUDEPATH += ../lib
    INCLUDEPATH += ../../Plugins
    
    #win32:LIBS             += -lsetupapi -ladvapi32 -luser32
    
    win32 {
        QT_PROJ_ROOTDIR = E:/Working/QtDev
    
        #### Copy the interface file of the plugin to the folder ####
        InterfaceFile = $$PWD/ipluginmanager.h
        # 将LibFile中的"/"替换为"\"
        InterfaceFile = $$replace(InterfaceFile, /, \\)
    
        #### Copy the library of the plugin to the folder ####
        LibraryFile = $$DESTDIR/EWhalesPluginManager.dll
        # 将LibFile中的"/"替换为"\"
        LibraryFile = $$replace(LibraryFile, /, \\)
    
        # 输出目录也是一样,要将"/"替换为"\"
        OutLibFile = $$QT_PROJ_ROOTDIR/Plugins/
        OutLibFile = $$replace(OutLibFile, /, \\)
    
        QMAKE_POST_LINK += "copy $$InterfaceFile $$OutLibFile"
        QMAKE_POST_LINK += "&& copy $$LibraryFile $$OutLibFile"
    }
    
    unix:!macx {
        QT_PROJ_ROOTDIR = /home/alwhales/QtDev
    
        #### Copy the interface file of the plugin to the folder ####
        InterfaceFile = $$PWD/ipluginmanager.h
    
        #### Copy the library of the plugin to the folder ####
        LibraryFile = $$DESTDIR/libEWhalesPluginManager.so
    
        # 输出目录也是一样,要将"/"替换为"\"
        OutLibFile = $$QT_PROJ_ROOTDIR/Plugins/
    
        QMAKE_POST_LINK += "cp $$InterfaceFile $$OutLibFile"
        QMAKE_POST_LINK += "&& cp $$LibraryFile $$OutLibFile"
    }
    
    macx {
        QT_PROJ_ROOTDIR = /Users/wangtao/QtProj
    
        #### Copy the interface file of the plugin to the folder ####
        InterfaceFile = $$PWD/ipluginmanager.h
    
        #### Copy the library of the plugin to the folder ####
        LibraryFile = $$DESTDIR/libEWhalesPluginManager.dylib
    
        # 输出目录也是一样,要将"/"替换为"\"
        OutLibFile = $$QT_PROJ_ROOTDIR/Plugins/
    
        QMAKE_POST_LINK += "cp $$InterfaceFile $$OutLibFile"
        QMAKE_POST_LINK += "&& cp $$LibraryFile $$OutLibFile"
    }
    
    RESOURCES += \
        resource.qrc
    
    HEADERS += \
        pluginmanager_panel.h
    
    include ($$PWD/update_tool/qsimpleupdater/QSimpleUpdater.pri)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91

    最终的项目目录结构为:
    在这里插入图片描述
    参考链接1:关于Qt Creator项目中.pro文件中的相对路径

    参考链接2:Qt Creator指定临时文件生成目录(MOC_DIR/RCC_DIR等)和PWD/OUT_PWD/.pro官方文档

    Chapter4 QT改变EXE生成路径,增加EXE图标,指定生成的EXE名称

    原文链接:https://blog.csdn.net/qq_28178271/article/details/131879509

    Chapter5 Qt 修改pro文件可以将生成exe或dll文件复制到多个地方

    原文链接:https://blog.csdn.net/hss2799/article/details/125299326

    直接上代码,在pro文件里面添加

    # 将给定文件复制到目标目录,相当于一个函数
    defineTest(copyToDestDir) {
        files = $$1
        dir = $$2
        # 替换Windows目标路径中的斜杠
        win32:dir ~= s,/,\\,g
    
        for(file, files) {
            # 替换Windows源路径中的斜杠
            win32:file ~= s,/,\\,g
    
            # 复制命令
            QMAKE_POST_LINK += $$QMAKE_COPY $$shell_quote($$file) $$shell_quote($$dir) $$escape_expand(\\n\\t)
        }
    
        export(QMAKE_POST_LINK)
    }
    #需要复制的文件清单,可以是exe,可以是dll,或其他的
    FROM_FILES += \
        $$PWD/../bin/UP5000/StartScreen.exe\
        $$PWD/../bin/UP5000/test.lib\
        $$PWD/../bin/UP5000/other.dll
    
    #复制到指定文件夹
    copyToDestDir($$FROM_FILES, $$PWD/../bin/UP5500/)
    copyToDestDir($$FROM_FILES, $$PWD/../bin/UR6000/)
    copyToDestDir($$FROM_FILES, $$PWD/../bin/UR8000/)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    FROM_FILES
    是需要复制的源文件清单

    $$PWD/…/bin/UP5500/
    是复制的目标路径,根据自己的需要,进行调整

  • 相关阅读:
    java标注
    linux命令之查找find整理、逻辑操作
    华为设备Smart Link和Monitor Link配置命令
    ElementUI 自定义 Tree 树形控件背景
    pytorch -- 构建自己的Dateset,DataLoader如何使用
    线程池线程保活以及动态更新线程数
    redis和nacos的安装
    pytorch,numpy两种方法实现nms类间+类内
    如何将用户导入自己的私域流量?“分享购“案例分享
    微信小程序获取openid流程
  • 原文地址:https://blog.csdn.net/m0_46577050/article/details/133970965