Qt5.0編譯指南(VisualStudio2010)


1 基礎環境

->關於Qt源代碼下載

Qt版本更替較快,一般在google里搜索Qt Download即可得到鏈接:https://qt-project.org/downloads ,可以在下面找到源代碼下載鏈接.可以看到Qt的下載站點類似ftp站點:Qt目前最新的源代碼包位於https://download.qt-project.org/official_releases/qt/5.0/5.0.2/single/,通過URL可以得到https://download.qt-project.org/提供了所有Qt相關的內容下載,下載Qt安裝包和源代碼包均可按圖索驥.

采用git下載源代碼編譯可以參考鏈接:https://qt-project.org/wiki/Building_Qt_5_from_Git.

->關於Qt的Windows平台需求

https://qt-project.org/doc/qt-5.0/qtdoc/requirements-win.html中描述了Qt的Windows平台需求,其中

  • ANGLE為替代OpenGL的圖形支持庫,ICU為UNICODE及國際化支持庫;
  • OpenSSL支持同樣依賴於外部庫.見https://qt-project.org/doc/qt-5.0/qtdoc/requirements.html
  • 編譯需要Perl,創建QtWebKit依賴與Python及Ruby;
  • jom為替代nmake的構建工具,可以利用多核CPU來加速構建;
  • 使用Windows SDK 7.1編譯時需要按照以下順序完成SDK安裝:
    • Install Visual Studio 2010
    • Install Windows SDK 7.1
    • Install Visual Studio 2010 SP1
    • Install Visual C++ 2010 SP1 Compiler Update for the Windows SDK 7.1

ICU為UNICODE支持庫,詳細信息參見:http://site.icu-project.org/,其中ICU4C為C/C++支持庫,ICU4J為Java支持庫,簡要介紹見:http://www.ibm.com/developerworks/cn/opensource/os-icu4c/,ftp下載站點為:http://download.icu-project.org/files/,針對MSVC10的32/64bit已編譯包在可從http://download.icu-project.org/files/icu4c/51.1/下載.

ANGLE庫可以從https://code.google.com/p/angleproject/獲取,但是由於其在DirectX9上實現,必須要安裝DirectX SDK.(https://www.microsoft.com/en-hk/download/details.aspx?id=6812).

其他工具下載鏈接:

Perl:http://www.activestate.com/activeperl/downloads

Python:http://www.python.org/download/

Rudy:http://rubyinstaller.org/

jom:https://qt-project.org/wiki/jom

2 Qt5.0模塊分類

Qt5.0模塊划分見https://qt-project.org/doc/qt-5.0/qtdoc/modules.html,可以根據所需模塊選擇性編譯:

->基礎模塊

Qt Core Core non-graphical classes used by other modules.提供給其他模塊使用的核心非圖形類 對應於源代碼目錄Qtbase
Qt GUI Base classes for graphical user interface (GUI) components. Includes OpenGL.圖形界面基礎類,包含OpenGL  
Qt Multimedia Classes for audio, video, radio and camera functionality.音頻、視頻、無線電、相機功能類  
Qt Network Classes to make network programming easier and more portable.網絡編程支持類 源代碼目錄qtmultimedia
Qt QML Classes for QML and JavaScript languages.QML及JavaScript支持類  
Qt Quick A declarative framework for building highly dynamic applications with custom user interfaces.使用自定義用戶界面構建高動態應用程序的描述性框架  
Qt SQL Classes for database integration using SQL.基於SQL的數據庫整合類  
Qt Test Classes for unit testing Qt applications and libraries.單元測試類  
Qt WebKit Classes for a WebKit2 based implementation and a new QML API. See also Qt WebKit Widgets in the add-on modules. 源代碼目錄qtwebkit
Qt WebKit Widgets WebKit1 and QWidget-based classes from Qt 4.  
Qt Widgets Classes to extend Qt GUI with C++ widgets.擴展Qt GUI的C++組件類  

->附加組件

 

Active Qt Classes for applications which use ActiveX and COM 源代碼目錄qtactiveqt
Qt Concurrent Classes for writing multi-threaded programs without using low-level threading primitives.  
Qt Graphical Effects Graphical effects for use with Qt Quick 2.  
Qt Image Formats Plugins for additional image formats: TIFF, MNG, TGA, WBMP.  
Qt OpenGL OpenGL support classes.

Note: Provided to ease porting from Qt 4.x. Please use the QOpenGL classes in QtGui for new code.

 
Qt Print Support Classes to make printing easier and more portable.  
Qt Declarative Qt Declarative is provided for Qt 4 compatibility. The documentation is available through the Qt 4.8 Qt Quickdocumentation. qtdeclarative
Qt Script Classes for making Qt applications scriptable. Provided for Qt 4.x compatibility. Please use the QJS* classes in the QtQmlmodule for new code.  
Qt Script Tools Additional components for applications that use Qt Script.  
Qt SVG Classes for displaying the contents of SVG files.  
Qt XML Patterns Support for XPath, XQuery, XSLT and XML schema validation. qtxmlpatterns

 

->工具

Qt Designer Classes for extending Qt Designer.
Qt Help Classes for online help.
Qt UI Tools Classes for UI tools.

3 配置選項

確保安裝Perl之后,在源代碼目錄下運行

configure -help

可以得到Qt的配置選項.主要選項如下:

目錄設置    
  -prefix <dir> 指定編譯結果目錄,如果不指定編譯結果將會出現在Qtbase下
庫編譯    
  -debug 生成debug庫
  -release 生成release庫
  -debug-and-release 生成debug及release庫
  -shared 創建並使用動態Qt庫
  -static 創建並使用靜態Qt庫
模塊編譯    
  -make <part> 編譯(libs,tools,examples...)
  -nomake <part> 不編譯(libs,tools,examples...)
  -skip <module> 構建時排除該模塊
  -no-widgets 禁止編譯widgets模塊
  -no-sql-<driver> 禁止編譯指定SQL驅動
  -no-opengl 不支持opengl
  -opengl <api>

使能opengl指定API

desktop 桌面OpenGL

es1

es2

  -icu 使用ICU庫
  -no-icu 不是用ICU
Windows支持    
  -no-vcproj 不生成vc++.vcproj文件
  -vcproj 只針對"win32-msvc.net",
  -no-openssl 不編譯openssl
...    

 

4 如何選擇模塊及配置

 源代碼目錄為: 

 Volume in drive D has no label.
 Volume Serial Number is 3245-ACDE

 Directory of D:\Qt\src

05/20/2013  09:48 PM    <DIR>          .
05/20/2013  09:48 PM    <DIR>          ..
05/20/2013  07:43 PM    <DIR>          .obj
04/09/2013  03:11 AM    <DIR>          gnuwin32
05/20/2013  07:58 PM    <DIR>          qtactiveqt
05/20/2013  07:57 PM    <DIR>          qtbase
04/09/2013  03:11 AM    <DIR>          qtdeclarative
04/09/2013  03:10 AM    <DIR>          qtdoc
04/09/2013  03:11 AM    <DIR>          qtgraphicaleffects
04/09/2013  03:11 AM    <DIR>          qtimageformats
04/09/2013  03:11 AM    <DIR>          qtjsbackend
04/09/2013  03:11 AM    <DIR>          qtmultimedia
04/09/2013  03:11 AM    <DIR>          qtquick1
05/20/2013  07:59 PM    <DIR>          qtscript
04/09/2013  03:11 AM    <DIR>          qtsvg
05/20/2013  08:06 PM    <DIR>          qttools
04/09/2013  03:10 AM    <DIR>          qttranslations
04/09/2013  03:11 AM    <DIR>          qtwebkit
04/09/2013  03:12 AM    <DIR>          qtwebkit-examples-and-demos
04/09/2013  03:11 AM    <DIR>          qtxmlpatterns
               0 File(s)              0 bytes
              20 Dir(s)  146,437,746,688 bytes free

 同樣,可以查看源代碼目錄下的qtsdk.pro文件

addModule(qtbase)
addModule(qtsvg, qtbase)
addModule(qtxmlpatterns, qtbase)
addModule(qtjsbackend, qtbase)
addModule(qtdeclarative, qtjsbackend, qtsvg qtxmlpatterns)
addModule(qtmultimedia, qtbase, qtdeclarative)
addModule(qtactiveqt, qtbase)
addModule(qtwebkit, qtdeclarative, , WebKit.pro)
addModule(qttools, qtbase, qtdeclarative qtactiveqt qtwebkit)
addModule(qtwebkit-examples-and-demos, qtwebkit qttools)
addModule(qtimageformats, qtbase)
addModule(qtgraphicaleffects, qtdeclarative)
addModule(qtscript, qtbase)
addModule(qtquick1, qtscript, qtsvg qtxmlpatterns qtwebkit qttools)
addModule(qttranslations, qttools)
addModule(qtdoc, qtdeclarative)

 在Qt5.0中可以選擇編譯的模塊為:

模塊名 所在目錄 描述
base qtbase 包含了Qt的基礎類庫,該類庫必須編譯
svg qtsvg Classes for displaying the contents of SVG files.顯示SVG文件內容的類庫
xmlpatterns qtxmlpatterns Support for XPath, XQuery, XSLT and XML schema validation
jsbackend qtjsbackend 構建依賴於Python
declarative qtdeclarative Qt Declarative is provided for Qt 4 compatibility. The documentation is available through the Qt 4.8 Qt Quick documentation.
multimedia qtmultimedia Classes for audio, video, radio and camera functionality.
activeqt qtactiveqt Classes for applications which use ActiveX and COM
webkit qtwebkit Classes for a WebKit2 based implementation and a new QML API. See also Qt WebKit Widgets in the add-on modules.
imageformats qtimageformats Plugins for additional image formats: TIFF, MNG, TGA, WBMP.
graphicaleffects qtgraphicaleffects Graphical effects for use with Qt Quick 2.
script qtscript Classes for making Qt applications scriptable. Provided for Qt 4.x compatibility. Please use the QJS* classes in the QtQml module for new code.
quick1 qtquick1 A declarative framework for building highly dynamic applications with custom user interfaces.
translations qttranslations tr翻譯功能
doc qtdoc  
webkit-examples-and-demos qtwebkit-examples-and-demos webkit的示例及演示
tools qttools
Qt Designer Classes for extending Qt Designer.
Qt Help Classes for online help.
Qt UI Tools Classes for UI tools.

 可以使用-skip modulename來跳過編譯該模塊.

  5 最小功能編譯

 假設只是為了使用Qt的GUI庫,也就是說,編譯只依賴於base+tools模塊,那么需要的工具為:

 ->Perl

 ->MSVC編譯環境

 ->jom&wtee(wtee可以保持命令行輸出並保存到文件,來給編譯過程進行記錄)

 可采用以下命令進行配置:

configure -prefix "D:\Qt\build" -release -opensource -nomake examples -no-opengl -no-openssl -no-openvg -no-angle -no-icu -no-vcproj -skip svg -skip xmlpatterns -skip jsbackend -skip declarative -skip multimedia -skip webkit -skip webkit-examples-and-demos -skip imageformats -skip quick1 -skip translations -skip doc -skip graphicaleffects  -mp -D _CRT_SECURE_NO_WARNINGS

 將編譯結果存儲在"D:\Qt\build"目錄,使用release模式編譯,不編譯exampes.不是用opengl、openssl、openvg,不是用angle及icu,-no-vcproj來禁止產生vc工程文件(如果配置為參數vc工程文件,在實際配置中會報出很多找不到vcproj文件的警告),除了base和tools模塊,其他全部跳過.使用-mp標志使能多處理器編譯支持,使用"-D _CRT_SECURE_NO_WARNINGS"宏傳遞選項來禁止關於string操作的windows 不安全警報(報出的很多警告都是該原因).

 配置無誤完成后.在qtbase\bin目錄下會有qmake、moc等編譯工具,用來編譯其他Qt模塊.然后使用如下命令進行編譯:

jom -j<處理器數目> install 

在配置及編譯時,假設將配置命令保存為cfg.bat,編譯命令保存為install.bat,使用wtee記錄中間的過程方式如下:

call cfg.bat |wtee cfg.log
call install.bat |wtee install.log

在I5-3320M 4核處理器,4G內存,64位Windows7操作系統上使用該配置編譯耗時17分鍾完成.在編譯完成之后,build\bin目錄下會有uitools編譯完成,可以打開designer.exe來檢查編譯結果是否正常.這樣編譯出來的lib即可作為發布使用.

 注:Perl安裝后自動設置環境變量,如果不確定,可以在命令行嵌入perl查看是否正常.

    :配置時最好從MSVC的命令行啟動,configure會根據你啟動的命令行判斷你所使用的編譯器版本及位數.

    :使用wtee及本文描述的.bat文件時,可以置於源代碼根目錄下

    :另外,如果使用-prefix指定編譯結果路徑,務必在編譯時使用install目標,使用默認目標將會在qtbase\bin目錄下創建build目錄保存編譯結果.

 6 配置引導

 ->我只是使用QtWidget來編寫GUI程序,不需要Webkit和Quick

 如果是這樣,安裝就不會依賴與ANGLE和ICU庫,同樣Rudy及Python也不需要,也無需安裝Direct SDK.你需要跳過webkit以及webkit-examples-and-demos,同時跳過jsbackend及quick1.並且采用-opengl desktop,以及 -no-angle標志.

 ->如何編譯32bit和64bit的Qt

 對於MSVC來講,編譯兩種Qt的不同支持僅僅在於啟動的編譯環境不同,其他配置均相同,當然你可能需要修改一下編譯輸出的路徑(清除上次編譯配置的命令為nmake confclean).

 ->關於debug與release

 配置時可以使用-release,-debug,-debug-and-release來使能相應的配置.

 ->靜態庫與動態庫

 配置時,使用-shared得到動態庫,-static得到靜態庫.

 ->多版本編譯及共存的建議

 可以使用QtCreator來選擇工具鏈,這樣,可以在build目錄下建立x86及x64兩個目錄,並在編譯時分別指定對應目錄,然后手動配置編譯工具鏈.

 7 常規應用編譯版本(不使用webkit及quick1)

 配置命令如下:

configure -prefix "D:\Qt\build\x64" -debug-and-release -opensource -opengl desktop -no-openssl -no-openvg -no-icu -no-vcproj -nomake examples -skip jsbackend -skip webkit -skip webkit-examples-and-demos -skip quick1 -skip declarative -skip multimedia -skip doc -skip graphicaleffects -mp -D _CRT_SECURE_NO_WARNINGS

注:關於模塊間的依賴關系可見qtsdk.pro下面的addmodule.

 該配置在I5-3320M 4核處理器,4G內存,64位Windows7操作系統上使用該配置編譯耗時30分鍾完成.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM