1. 程式人生 > >QT5.10開發(2) 在Windows 10下使用MinGW編譯 靜態Qt 5.10 release版 詳細過程

QT5.10開發(2) 在Windows 10下使用MinGW編譯 靜態Qt 5.10 release版 詳細過程

Qt建議安裝動態連結Dbug版和編譯安裝靜態連結release版

前提:

先安裝動態連結Dbug版,方法:QT5.10開發(1)安裝QT5.10

地址:http://blog.csdn.net/qq_15304853/article/details/79165301

然後編譯靜態連結release版,方法:

1 準備工作
1.1 編譯環境

Windows 10 (Multiple Editions), Version 1703 (Updated July 2017) (x64)


1.2 建立資料夾結構

假設之前已安裝Qt存放在D:pg-Qt目錄下,該目錄此時內容應如下:



進入5.10.0目錄,新建目錄:mingw53_32_static 和 qt_build_mingw53_32_static,結果如下


1.3 準備原始碼

如果之前安裝時選擇連原始碼一起安裝,則此時原始碼已經在:D:\pg-Qt\5.10.0\Src目錄下了,參見上圖。

如果為選擇連原始碼一起安裝,則可從Qt官網下載:http://download.qt.io/official_releases/qt/5.10/5.10.0/single/   下載的qt原始碼包“qt-everywhere-src-5.10.0.tar.xz”,解壓後,放在硬碟的根目錄。例如:“F:\qt-everywhere-src-5.10.0 ”


注:如果qt原始碼目錄的路徑比較長會導致編譯qtimageformats時出現 無法開啟編譯器生成的檔案“”:Invalid argument的錯誤,所以放在根目錄下,儘量減少路徑長度。
2 安裝編譯所需的工具(直接在360軟體寶庫搜尋安裝)


1、python 2 ,官網“https://www.python.org/downloads/”  本次測試編譯版本為python-2.7.13。

不安裝在配置qt時會提示:WARNING: Python version 2 (2.7.5 or later) is required to build QtWebEngine.


2、Ruby ,官網“http://www.ruby-lang.org/en/downloads/”  本次測試編譯版本為Ruby-1.9.2。

3   ActivePerl,官網“https://www.activestate.com/activeperl/downloads”本次測試編譯版本ActivePerl-5.24.1.2402

如果不編譯qtwebkit,就不用安裝ActivePerl了。

注意:安裝時必須選擇“建立環境變數”選項。,也可以手動建立,我的如下:



3 配置Qt
3.1準備openssl
下載別人專門為Qt編譯好的openssl,地址 http://www.cnblogs.com/findumars/p/6372223.html,下載後解壓OpenSSL的zip壓縮包(openssl-1.0.1c_static_w32_mingw.zip)後放置:D:pg-Qt目錄下,此時路徑為:D:pg-Qt\openssl-1.0.1c_static_w32_mingw

也可以自己編譯,參考:

靜態編譯OpenSSL並整合到Qt  

http://blog.csdn.net/lixiaoxin1989/article/details/78421573

Windows 下編譯 OpenSSL  

http://blog.csdn.net/liang19890820/article/details/51658574

openSSL1.1.1的編譯

http://blog.csdn.net/lostspeed/article/details/69492531


3.2
開啟終端,開始==》程式==》Qt5.10.0==》Qt 5.10.0 for Desktop (MinGW 5.3.0 32 bit)進入命令視窗


3.3 檢查命令執行情況

以管理員使用者執行windows 命令列。
1)執行命令“gcc –v”  確保編譯器能夠正常執行;
2)執行命令“perl –v”確保perl能夠正常執行;
3)執行命令“ruby  –v”確保ruby能夠正常執行;
4)執行命令“python”確保python能夠正常執行,按“Ctrl+Z”結束執行;
5)執行命令“sh”確保該命令不能執行,其會影響ming32-make的執行。

3.4使用shadow-build配置Qt
 輸入命令:

D :
cd D:\pg-Qt\Qt5.10.0\qt_build_mingw53_32_static
在該目錄下執行下面命令:

F:\qt-everywhere-src-5.10.0\configure.bat  -static -release -confirm-license  -opensource -opengl desktop -platform win32-g++  -prefix "D:\pg-Qt\5.10.0\mingw53_32_static" -sql-sqlite  -sql-odbc -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -make libs  -nomake tools -nomake examples -nomake tests -openssl-linked  OPENSSL_LIBS="-llibssl -llibcrypto -lgdi32" -L D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw -I D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw\include-skip qt3d  -skip qtcanvas3d -skip qtdatavis3d -skip qtlocation -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview -skip qtwinextra

1)如果不需要包含OpenSSL,去掉-openssl-linked  OPENSSL_LIBS="-llibssl -llibcrypto -lgdi32" -L D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw -I D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw\include

2)我跳過了一下包,如果你需要這些,去掉-skip qt3d  -skip qtcanvas3d -skip qtdatavis3d -skip qtlocation -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview -skip qtwinextra



引數說明:

-confirm-license -opensource  :使用開源許可

-opengl desktop :桌面版

-platform win32-g++  :生成的編譯器型別為:windows C++編譯器

-release  :生成的編譯器可編譯產品: 釋出版

-static  :生成的編譯器為 靜態連結編譯器

-static-runtime:生成的編譯器使用靜態執行時庫

-nomake tools:不編譯tools

-prefix "5.10.0-desktop-mingw53-rel-static"  :生成的編譯器安裝位置


注:

如果出錯,將D:\pg-Qt\5.10.0\qt_build_mingw53_32_static下檔案全部刪除,重新configure

1)通過shadow-build方法配置可實現原始碼和編譯產生的檔案如Makefiles, object files和其他中間檔案的徹底分離。編譯檔案存放在:D:\pg-Qt\5.10.0\qt_build_mingw53_32_static

2) 也可以直接切換到F:\qt-everywhere-src-5.10.0\目錄,

3) 然後執行:configure.bat  -static -release -confirm-license  -opensource -opengl desktop -platform win32-g++  -prefix "D:\pg-Qt\5.10.0\mingw53_32_static "  -sql-sqlite  -sql-odbc -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -make libs  -nomake tools -nomake examples -nomake tests -openssl-linked  OPENSSL_LIBS="-llibssl -llibcrypto -lgdi32" -L D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw -I D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw\include-skip qt3d  -skip qtcanvas3d -skip qtdatavis3d -skip qtlocation -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview -skip qtwinextra

4) 但產生編譯檔案在F:\qt-everywhere-src-5.10.0\中

配置沒有錯誤後的輸出結果類似下圖,不過最好檢查自己需要的模組是否為yes:


4 編譯Qt庫

執行下面命令編譯Qt庫

mingw32-make

過程較長,我安裝時沒有跳過東西,用了8小時!

注:若前面編譯過Qt庫,需執行下面命令,mingw32-make clean清除(該方法為官方文件推薦方法,本人推薦直接刪除“D:\pg-Qt\\5.10.0\qt_build_mingw53_32_static”資料夾下的全部內容重新配置,這樣更徹底、快速)。

5 安裝Qt

mingw32-make install


6 配置Qt庫

1. 把編譯的靜態Qt庫放到  動態的Qt庫所在目錄,直接複製動態Qt庫\bin目錄下的qt.conf檔案,或在靜態庫\bin目錄下建立qt.conf檔案,裡面輸入以下內容:
    [Paths] 
    Prefix=.. 

2. 開啟D:\pg-Qt\5.10.0\mingw53_32_static\mkspecs\win32-g++\qmake.conf,

         找到 QMAKE_LFLAGS_DLL

         將後面都改成 -static

         再加一行 QMAKE_LFLAGS

         後面也改成 -static        

結果:

QMAKE_LFLAGS               = -static
QMAKE_LFLAGS_EXCEPTIONS_ON = -mthreads
QMAKE_LFLAGS_EXCEPTIONS_OFF =
QMAKE_LFLAGS_RELEASE    = -Wl,-s
QMAKE_LFLAGS_DEBUG      =
QMAKE_LFLAGS_CONSOLE    = -Wl,-subsystem,console
QMAKE_LFLAGS_WINDOWS    = -Wl,-subsystem,windows
QMAKE_LFLAGS_DLL        = -static

3. 複製動態庫中的bin/qmlscene.exe到靜態庫的bin資料夾中。否則在Qt Creator中新增Qt庫時會報qmlscene未安裝的錯誤。

4. 從動態連結版的Qt庫的bin資料夾中複製Qt5Core.dll到靜態連結版的lib資料夾中。

注:① 如果不執行此步,在Creator的Qt Version中將會顯示“x86-windows-unknown-pe-32bit”。

5. 拷貝qml檔案

因為一些我還沒了解的原因,編譯出來的Qt,在D:\pg-Qt5.10.0\mingw53_32-static\qml\QtQuick\Controls目錄下是沒有.qml檔案,如下圖,左邊為普通的版本,右邊為靜態編譯版本Qt。右邊qml檔案一個都沒有。所以,這些qml檔案都需要手動拷貝下。



7 整合到Qt Creator 中

工具--選項--構建和執行

在Qt Versions選項卡中新增D:\pg-Qt\5.10.0\mingw53_32_static\bin\qmake.exe靜態配置後,點選“Apply”按鈕。

在Kit選項卡中按下圖配置。



8使用該 “windows靜態連結庫 ”套件編譯出項目的release版靜態連結軟體

建立或開啟專案-進入專案-點選“專案”-配置使用該庫

點選-執行。


參考資料

(1) http://doc.qt.io/qt-5/windows-support.html
(2) http://doc.qt.io/qt-5/windows-requirements.html
(3) http://doc.qt.io/qt-5/windows-building.html
(4) http://doc.qt.io/qt-5/configure-options.html
(5) http://blog.csdn.net/lixiaoxin1989/article/details/53838277

(6) http://blog.csdn.net/wsj18808050/article/details/72862478

http://blog.csdn.net/wsj18808050/article/details/72862478