官方參考文檔:https://doc.qt.io/qt-5/build-sources.html
CSDN博客:https://blog.csdn.net/GG_SiMiDa/article/details/78517246
博客園博客:https://www.cnblogs.com/BuildingIT/archive/2013/03/22/2976480.html
安裝QT有兩種方式:
- 通過QT安裝包(Windows下exe文件)
- 通過QT源碼
下文主要描述QT源碼的編譯方法,QT版本為5.12
下載源碼:http://download.qt.io/archive/qt/5.12/5.12.0/single/
PS:我下載時遇到的一個插曲,直接在瀏覽器中點擊紅線處下載的zip文件解壓不了,提示損壞了,最后右鍵菜單"復制鏈接地址"使用迅雷精簡版下載后可以正常解壓使用!
Windows平台相關支持需要:
我解壓到F:\qt-everywhere-src-5.12.0,可以看到里面有configure文件,在此處使用 configure -h 可以看到可配置的參數,太多了這里只列舉幾個比較常設置的:
-release Compile and link Qt with debugging turned off.
-debug Compile and link Qt with debugging turned on.
-debug-and-release Build two versions of Qt.
-shared Build shared Qt libraries [yes] (no for UIKit)
-static Build static Qt libraries [no] (yes for UIKit)
-nomake tests Disable building of tests to speed up compilation
-nomake examples Disable building of examples to speed up compilation
-confirm-license Automatically acknowledge the LGPL 2.1 license.
-no-opengl Disable OpenGL support
-opengl <api> Enable OpenGL support.Support APIs:
es2 <default on Windows>
desktop <default on Unix>
dynamic <Windows only>
-opengles3 Enable OpenGL ES3.x support instead of ES2.x [auto]
Windows編譯需要准備如下工具:
Python 2.7及以上版本(如果編譯WebKit >=2.6.x):下載地址https://www.python.org/downloads/
Perl 5.12 以上版本(必須安裝且版本>=5.14):下載地址https://www.activestate.com/activeperl/downloads
支持C++11的編譯器,MSVC2012以上版本或者MinGW4.9以上版本
Qt編譯有兩種控制方式:
1、取決於是否使用什么層次的OpenGL API:這里有三個選擇:-opengl desktop、-opengl es2和-no-opengl,將來還會增加-opengl es3;
2、如果選用OpenGL ES 2的API渲染的話,Qt也給出了三種方案:-no-angle,直接使用OpenGL的API進行渲染,需要顯卡廠商支持OpenGL ES 2.0,對應libGLESv2.dll、libEGL.dll;-angle,如果客戶機器不支持OpenGL ES 2.0渲染,但是支持OpenGL 1.5,或者支持DirectX 9.0,那么可以使用-angle這個解決方案轉換為DirectX的渲染API進行渲染,需要D3DCompiler_4(x).dll以及libGLESv2.dll、libEGL.dll;-angle-d3d11,如果客戶機器支持DirectX 11(需要Windows 7以上,通常情況也支持OpenGL ES 2.0),想用最新的DirectX API進行渲染,那么可以使用這個方法進行編譯。
以編譯靜態庫為例
Windows:
msvc版
configure -confirm-license -opensource -platform win32-msvc -debug-and-release -static -static-runtime -force-debug-info -opengl dynamic -prefix "./build" -qt-sqlite -qt-pcre -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -qt-freetype -nomake tests -no-compile-examples -nomake examples
mingw版
configure -confirm-license -opensource -platform win32-g++ -debug-and-release -static -static-runtime -force-debug-info -opengl dynamic -prefix "./build" -qt-sqlite -qt-pcre -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -qt-freetype -nomake tests -no-compile-examples -nomake examples
Linux:
./configure -confirm-license -opensource -debug-and-release -static -static-runtime -force-debug-info -opengl dynamic
[我的配置-mingw版]
configure -prefix "./build" -release -opensource –static -static-runtime -force-debug-info -opengl dynamic -opengl desktop -platform win32-g++ -c++std c++11 -skip qtwebengine -nomake examples -nomake tests -mp -confirm-license
配置完成后會生成Makefile文件
[nmake/mingw32-make/make]
使用對應平台下的編譯工具(nmake是MSVC的make,mingw32-make是g++的Windows版本的make,make是Linux中的make),如果沒有請安裝好,此過程比較長,大概一個小時以上,漫長的等待中……
[nmake/mingw32-make/make] install
安裝成功后,應該可以在./build目錄下看到如下文件夾:
如果是靜態編譯,在lib中可以看到.a庫,動態的則是.dll或者.so庫
在bin目錄下,qt助手、qt設計師、qt翻譯家等也都編譯出來了~
但是沒有qt creater,這需要單獨下載qt creater的源碼單獨編譯!
QT靜態庫的使用:
打開QT Creater,運行qt自帶的例子boxes,使用默認的MinGW配置編譯會編譯不過,提示錯誤: This example requires Qt to be configured with -opengl desktop
原因是默認的Windows版本的QT使用的編譯選項是-opengl dynamic,而boxes例子中使用了原生的opengl繪圖,需要-opengl desktop才能使用。我們上述編譯的qt靜態庫特意配置了-opengl desktop選項。
"工具"->"選項"->"Kits":
添加Qt Versions,將我們上述編譯的qt靜態庫build目錄中的qmake添加:
配置構建套件(Kit),手動設置"桌面"配置如下:
打開boxes工程,配置"桌面"編譯選項,執行build操作
等待大約2分鍾左右,boxes例程終於可以成功編譯了~
運行吧!