1. 為什么要這么做
在Windows下,Qt官網默認提供的二進制版本大多是32位,目前(2015.01.24)只對VS2013提供了64位版本,如下圖所示:

為了適應我目前安裝的VS2010 IDE,也為了根據自己的需要編譯特定的庫和插件(比如去掉webkit之類),需要直接從源碼編譯Qt。
本例編譯的是Qt5.3.2版本,應該也適用於Qt5.4.0。
2. 准備
首先,當然需要一個C++編譯器,由於一些兼容性原因,我需要用Visual C++ 2010, 也可以用MinGW。用MinGW的話還對調試有幫助,這是后話。
另外在Windows上編譯Qt需要一些庫,參見http://doc.qt.io/qt-5/windows-requirements.html。
由於我不需要3D圖形,WebKit等支持,因此不需要Opengl, ANGLE, ICU等一堆庫,只需要以下3個編譯時要用的工具(運行時不需要):
- ActivePerl - Install a recent version of ActivePerl (download page) and add the installation location to your
PATH. - Python - Install Python from the here and add the installation location to your PATH in order to be able to build Qt WebKit.
- Install Ruby from here and add the installation location to your PATH in order to be able to build Qt WebKit.
把這3個玩意兒下載安裝上即可。
3. 編譯
把下載下來的Qt源碼解壓至目錄<QtDir>,然后在此目錄根下創建一個批處理文件,內容如下:
@echo off ECHO 設置 Visual Studio environment... CALL "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" amd64 title Command Prompt (MSVC++ 2010) x64 SET QTSRC_DIR=H:\qt-msvc2010-x64 SET QMAKESPEC=win32-msvc2010 SET PATH=%PATH%;%QTSRC_DIR%\qtbase\bin;%QTSRC_DIR%\gnuwin32\bin SET PATH=%PATH%;C:\Python27;C:\Perl64\bin;C:\Ruby21-x64\bin; ECHO 按任意鍵,進行configure... @PAUSE>NUL CALL configure -opensource -mp -debug-and-release -nomake examples -no-opengl -no-angle -skip qtquick1 -skip qtquickcontrols -skip qtsensors -skip qtwebkit ECHO 按任意鍵,開始編譯... @PAUSE>NUL CALL jom.exe -j 4 REM 重新配置和編譯請使用 REM nmake distclean 或jom clean goto :eof
其中設置好了VC++環境變量、源碼目錄、必備工具執行目錄等等;
通過configure設置了需要編譯的模塊和不需要編譯的模塊。通過禁止不需要的編譯模塊,可以大幅加快編譯速度;
jom.exe -j 4指定使用4個線程並行編譯。
寫好保存為bat文件,在命令行執行即可。
