Qt最新版5.14在Windows環境靜態編譯安裝和部署的完整過程 VS 2019-Qt static link build Windows 32 bit/64 bit

此文首發於我的Jekyll博客:Qt 5.14 - zhang0peter的個人博客

爲什麼要靜態編譯(static link)

在默認情況下,用QtCreator編譯程序時,使用的是動態編譯。編譯好的程序在運行時需要另外加上相應的Qt庫文件,一大堆dll文件。如果想將生成的程序連同所需要的庫一起完整地打包成一個可執行程序,那就需要使用靜態編譯。Qt官方不提供靜態編譯好的安裝包,所以需要我們在自己的電腦上靜態編譯。而且exe文件會比動態編譯的要小上很多。

1.源碼下載/source code download

2020-2-25: Qt 5.14.1 版本出來

Qt最新版5.14.1的源碼下載地址:Index of /archive/qt/5.14/5.14.1/single

選擇文件:qt-everywhere-src-5.14.1.zip

源碼壓縮包849M,解壓後2.32G

2. 編譯工具下載/compiler download

Qt官方文檔要求編譯工具:Qt for Windows - Requirements Qt 5.14

打開源碼目錄下的README文件,查看當前版本Qt要求的Windows環境下編譯需要安裝的工具:

   Windows:
   --------

     Open a command prompt.
     Ensure that the following tools can be found in the path:
     * Supported compiler (Visual Studio 2012 or later,
        MinGW-builds gcc 4.9 or later)
     * Perl version 5.12 or later   [http://www.activestate.com/activeperl/]
     * Python version 2.7 or later  [http://www.activestate.com/activepython/]
     * Ruby version 1.9.3 or later  [http://rubyinstaller.org/]

編譯環境選擇:MinGW/MSVC

在Windows上,有兩個預構建環境可供選擇:一個是 MinGW ,另一個是Microsoft Visual Studio(MSVC)。這兩個環境不兼容,無法混合。你必須選擇一個。

這兩者的區別如下:

當你的項目使用MinGW編譯的使用,想要用一個MSVC編譯生成的庫時就會有問題。使用MinGW編譯項目的時候,所使用的Lib也要是MinGW編譯的。如果你只是開發Window平臺的軟件時,最好用Qt MSVC組合,這樣可以使用大量的第三方lib,還有很多的構建指令,畢竟window上MSVC纔是王道。

我選擇MSVC,打開安裝VS時自帶安裝的MSCV:x64 Native Tools Command Prompt for VS 2019

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.4.5
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>

默認編譯的結果是64位的Qt

如果你想編譯32位版本的qt,可以選擇x86 Native Tools Command Prompt for VS 2019

警告:不要使用 Developer Command Prompt for VS 2019

如果你想在linux系統中進行Qt的編譯工作,參考這個問答:c++ - How to compile Qt 5 under Windows or Linux, 32 or 64 bit, static or dynamic on Visual Studio or g++ - Stack Overflow

Perl

Perl 官網:Perl Download - www.perl.org

其中Windows版有2種可以下載,分別是ActivePerlStrawberry Perl。其中ActivePerl需要註冊後下載,Strawberry Perl可以直接下載,推薦Strawberry Perl.

Strawberry Perl下載地址:Strawberry Perl for Windows

安裝時默認會添加ActivePerl到環境變量。

測試添加是否成功:

Windows PowerShell
版權所有 (C) Microsoft Corporation。保留所有權利。

嘗試新的跨平臺 PowerShell https://aka.ms/pscore6

PS C:/Users/peter> perl.exe -v

This is perl 5, version 30, subversion 1 (v5.30.1) built for MSWin32-x64-multi-thread

Copyright 1987-2019, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

Python

下載Python:Python Releases for Windows Python.org

推薦下載最新版3.8.1

安裝後手動添加Python到環境變量。

測試添加是否成功:

Windows PowerShell
版權所有 (C) Microsoft Corporation。保留所有權利。

嘗試新的跨平臺 PowerShell https://aka.ms/pscore6

PS C:/Users/peter> python.exe
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated.  Libraries may fail to load.  To activate this environment
please see https://conda.io/activation

Type "help", "copyright", "credits" or "license" for more information.
>>>

Ruby

Ruby下載地址:RubyInstaller for Windows

安裝時默認會添加Ruby到環境變量。

測試添加是否成功:

PS C:/Users/peter> ruby.exe -v         
ruby 2.6.5p114 (2019-10-01 revision 67812) [x64-mingw32]

版權聲明:本文爲博主原創文章,轉載請附上博文鏈接:Qt最新版5.14在Windows環境靜態編譯安裝和部署的完整過程 VS 2019-Qt static link build Windows

3.編譯

在D盤新建一個目錄用來存放編譯好的內容:D:\qt

x64 Native Tools Command Prompt for VS 2019的終端進入Qt源代碼目錄:

C:\Users\peter\Downloads>cd qt-everywhere-src-5.14.1
C:\Users\peter\Downloads\qt-everywhere-src-5.14.1>

1.修改源碼裏的qtbase\mkspecs\common\msvc-desktop.conf文件

修改-MD爲-MT

修改前:

QMAKE_CFLAGS_RELEASE    = $$QMAKE_CFLAGS_OPTIMIZE -MD
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_OPTIMIZE -Zi -MD
QMAKE_CFLAGS_DEBUG      = -Zi -MDd

修改後:

QMAKE_CFLAGS_RELEASE    = $$QMAKE_CFLAGS_OPTIMIZE -MT
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $$QMAKE_CFLAGS_OPTIMIZE -Zi -MT
QMAKE_CFLAGS_DEBUG      = -Zi -MTd

D的意思是動態編譯(dynamic link),T的意思是靜態編譯(static link)。
這一步官方教程沒有要求,但其他人的很多教程裏都有寫,我就加上了

2.配置config文件:

配置命令如下:

configure.bat -static -prefix "D:\qt" -confirm-license -opensource  -debug-and-release -platform win32-msvc  -nomake examples -nomake tests  -plugin-sql-sqlite -plugin-sql-odbc -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -mp

具體含義如下:

configure.bat 
-static #指明是靜態編譯
-prefix "D:\qt" #指明安裝的目錄
-confirm-license -opensource  #指明是開源版本的qt
-debug-and-release #指明需要debug版和release版,可以單獨選擇release版
-platform win32-msvc  #指明使用msvc編譯,這裏的win32並不指32位
-nomake examples -nomake tests  #不編譯樣例
-plugin-sql-sqlite -plugin-sql-odbc -qt-zlib -qt-libpng -qt-libjpeg #可選插件
-opengl desktop  #用系統自帶的opengl
-mp #多核編譯
C:\Users\peter\Downloads\qt-everywhere-src-5.14.1>configure.bat -static -prefix "D:\qt" -confirm-license -opensource  -debug-and-release -platform win32-msvc  -nomake examples -nomake tests  -plugin-sql-sqlite -plugin-sql-odbc -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -mp
+ cd qtbase
+ C:\Users\peter\Downloads\qt-everywhere-src-5.14.1\qtbase\configure.bat -top-level -static -prefix "D:\qt" -confirm-license -opensource  -debug-and-release -platform win32-msvc  -nomake examples -nomake tests  -plugin-sql-sqlite -plugin-sql-odbc -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -mp

Notice: re-mapping requested qmake spec to unified 'win32-msvc'.

Bootstrapping qmake ...
....
Qt is now configured for building. Just run 'nmake'.
Once everything is built, you must run 'nmake install'.
Qt will be installed into 'D:\qt'.

Prior to reconfiguration, make sure you remove any leftovers from
the previous build.

3.make

上面提到過在Windows上,有兩個預構建環境可供選擇:一個是MinGW,另一個是Microsoft Visual Studio(MSVC)。

我選擇的是msvc環境,使用命令nmake即可進行編譯。

nmake

新版的MSVC可以進行多線程編譯,舊版本的MSVC自帶的nmake沒有多線程編譯功能,有一個替代功能的軟件jom可以進行nmake的多線程編譯。

jom的下載地址:Jom - Qt Wiki

在剛纔的終端中運行命令:

D:/jom_1_1_3/jom.exe  

我的CPU是i7-8565U,CPU100%運行,花費了大約3個小時。

make完成後源碼和編譯後的共有26G

4.make install

使用nmake單線程install:

nmake install

安裝完成後,QT的文件夾有4G

4.添加到Qt Creator

這裏需要說明一下,Qt Creator現在已經不能直接下載最新版本,需要在線安裝:Index of /archive/online_installers/3.2

下載後安裝最新版的Qt Creator4.9

添加Qt Versions

新建構建套件

新建構建套件,Qt版本選擇剛添加的靜態版,記得編譯器選自己編譯時用的編譯器。

在這裏插入圖片描述

如果編譯器版本選錯了,會報錯:

qwindowsvistastyled.lib(main.obj):-1: error: LNK1112: 模塊計算機類型“x86”與目標計算機類型“x64”衝突
:-1: error: Symbolic links not supported: disabling -L.

注意:如果選擇的編譯器跟跟編譯qmake時的編譯器不同,會報很多錯誤

如果你不知道自己的編譯器是哪個版本,全部試一遍

新建項目

新建項目,成功運行:

在這裏插入圖片描述

一個exe的大小是13M,包括了所有需要的內容。

5.小結

Qt 5.14.1 的靜態編譯的過程還算順利,需要注意的是編譯前要仔細閱讀官方文檔,畢竟很多教程不是最新的,但官方文檔一般都是最新的。

我把編譯好的qt放到了百度網盤上,有需要的可以下載,不保證能用:

鏈接: https://pan.baidu.com/s/1qCWI5uYhWvwHVptFSSTEbw 提取碼: 2333

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章