官方參考文檔:https://doc.qt.io/qt-5/build-sources.html html
CSDN博客:http://www.javashuo.com/article/p-qnkpctlj-ks.htmlpython
博客園博客:https://www.cnblogs.com/BuildingIT/archive/2013/03/22/2976480.html c++
安裝QT有兩種方式:web
下文主要描述QT源碼的編譯方法,QT版本爲5.12 sql
下載源碼:http://download.qt.io/archive/qt/5.12/5.12.0/single/ api
PS:我下載時遇到的一個插曲,直接在瀏覽器中點擊紅線處下載的zip文件解壓不了,提示損壞了,最後右鍵菜單"複製連接地址"使用迅雷精簡版下載後能夠正常解壓使用!瀏覽器
Windows平臺相關支持須要:工具
我解壓到F:\qt-everywhere-src-5.12.0,能夠看到裏面有configure文件,在此處使用 configure -h 能夠看到可配置的參數,太多了這裏只列舉幾個比較常設置的:ui
-release Compile and link Qt with debugging turned off. spa
-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編譯有兩種控制方式:
一、取決因而否使用什麼層次的OpenGL API:這裏有三個選擇:-opengl desktop、-opengl es2和-no-opengl,未來還會增長-opengl es3;
二、若是選用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例程終於能夠成功編譯了~
運行吧!