在 Windows 10 编译 Qt 5.9 源代码的详细过程 步骤详解


以下编译过程仅适于 Qt 5.9 及其子版本,经测试可放心使用 (意外情况除外)。

获取源码


从 Qt 官网或 国内镜像 下载源代码。

注意: 从 Qt 官网 git 下载的源代码可能无法使用,因为经常修改可能不稳定。

git 获取源码 --> cmd --> X: --> cd ./Qt --> git clone git://code.qt.io/Qt/qt5.git --> cd ./qt5 --> git checkout 5.9 --> git submodule update --init --recursive

git 更新源码 --> cmd --> X: --> cd ./Qt/qt5 --> git remote -v --> git fetch origin --> git log -p 5.9 origin/5.9 --> git merge origin/5.9 --> git submodule update

安装 Windows SDK


安装 Windows SDK 10.0.18362 (编译 qtwebengine 时需要) --> 安装位置 C:\Program Files (x86)\Windows Kits\10\ --> 默认安装所有 Features

成功完成后,添加以下一些环境变量:

把 C:\Program Files (x86)\Windows Kits\10\bin\;C:\Program Files (x86)\Windows Kits\10\lib\;C:\Program Files (x86)\Windows Kits\10\Include\; 添加到 path 环境变量

把 C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64\;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\um\x64\; 添加到 path 环境变量

把 C:\Program Files (x86)\Windows Kits\10\Redist\D3D\x64\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\; 添加到 path 环境变量

把 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Bin\x64\;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64\; 添加到 path 环境变量

安装 Windows DirectX SDK


安装 DXSDK_Jun10_9.29.1962.exe

在编译 ANGLE 时需要 DirectX 11 (仅适于 Windows 7),Windows SDK 10.0.18362 带有 DirectX 12 (通过 dxdiag 命令查看版本)。

位置 C:\Program Files (x86)\Microsoft DirectX SDK (June 2010) --> 把 C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Utilities\bin\x64\; 添加到 path 环境变量

安装 Visual Studio


Visual Studio Community 2015 with Update 3 (x86 and x64) 英文版 (非英文版编译时, 某些字符由于存在兼容性问题可能出错)

位置 X:\MSVC\Microsoft Visual Studio 14.0 --> 勾选 Programming Languages 下 Visual C++ 下 Common Tools for Visual C++ 2015 --> 去除其它非必要安装项

安装 qmake


安装 qt-opensource-windows-x86-5.x.exe (或带 qmake 的其它 *.EXE 二进制发行版) --> 安装时需要填入 Qt 用户账号 (没有账号需要先申请) -->

安装位置 X:\Qt\Qt5.x --> 成功完成后,添加以下环境变量:

把 C:\Windows;C:\Windows\System32; 和 X:\Qt\Qt5.x\5.x\msvc201x_64\bin\; 添加到 path 环境变量头部

查看版本 --> cmd --> qmake -v

安装 Python 2.7


安装 Python 2.7.9 或以上版本 (不支持 Python 3.x,编译 qtwebengine 时需要)

位置 X:\Python27 --> 勾选 Add python.exe to Path 环境变量 --> 把 F:\Python27\;F:\Python27\Scripts\; 添加到 path 环境变量

查看版本 --> cmd --> python -v

安装 ActivePerl


安装 ActivePerl 5.22 或以上版本

把 C:\Perl64\bin\;C:\Perl64\lib\; 及 C:\Perl64\site\bin\;C:\Perl64\site\lib\; 添加到 path 环境变量

查看版本 --> cmd --> perl -v

安装 GnuWin32


到 https://sourceforge.net/projects/getgnuwin32 下载 GetGnuWin32-0.6.3.exe --> 安装位置 X:\GnuWin32 --> 把 X:\GnuWin32\bin\;X:\GnuWin32\lib\; 添加到 path 环境变量

到 https://sourceforge.net/projects/winflexbison 下载 win_flex_bison3-latest.zip --> 解压到 X:\win_flex_bison 目录下 --> 把 X:\win_flex_bison\; 添加到 path 环境变量 --> 若在 qt5.git 之外构建 qtbase,把 win_bison 和 win_flex 重命名为 bison 和 flex

到 https://sourceforge.net/projects/gnuwin32 下载 bison-2.4.1 --> 安装位置 X:\GnuWin32 --> 安装 flex-2.5.4a-1 --> 安装 gperf-3.0.1 --> 安装 grep-2.5.4 --> 安装 libiconv-1.9.2-1

编译 ANGLE QtWebEngine 时需要 Bison、Flex、GPerf

若以上 GnuWin32 链接无法下载,请转至 国内镜像

安装 Win64OpenSSL


注意:OpenSSL 版本不通过低也不能过高,否则无法使用。

安装 Win64OpenSSL 1.1.0 以下版本 (OpenSSL 1.1 及之后版本,尚不兼容)

位置 X:\OpenSSL-Win64 --> 勾选 Copy OpenSSL DLLs to: The OpenSSL binaries (/bin) directory

把 X:\OpenSSL-Win64\bin\;X:\OpenSSL-Win64\lib\; 添加到 path 环境变量

安装 ICU4C


下载 icu4c-59.1-Win32&64-MSVC2015.7z --> 解压 icu4c-59.1-Win32&64-MSVC2015.7z 到 X:\icu4c

把 X:\icu4c\bin\;X:\icu4c\lib\; 添加到 path 环境变量

区域设置


qtwebkit 编译设置。

控制面板 --> 时间、语言和区域 --> 区域 --> 更改位置 --> 管理 --> 更改系统区域设置 --> 英语(美国)--> 确定 --> 重启

比对源码


解压 qt-everywhere-opensource-src-5.9.0.zip 到 X:\Qt\qt-everywhere-opensource-src-5.9.0 目录下 (注意:此硬盘分区 100GB 以上)

使用 数据比较 工具,比对 qt-opensource-windows-x86-5.9.0.exe 安装包自带源码 (查看源码是否差异太大,是否存在问题)

安装 jom


下载并安装 jom_1_1_3.zip (jom 编译效率更高相比 nmake)

解压到 X:\jom 目录下 --> 把 X:\jom\; 添加到 path 环境变量

编译 Qt5.9


调用 Visual Studio 2015 x64 Win64 Command Prompt (如有更新环境变量, 需重启此窗口) --> X: --> cd .\Qt\qt-everywhere-opensource-src-5.9.0

共享版 --> configure -confirm-license -opensource -platform win32-msvc2015 -release -mp -prefix "X:\Qt\Qt5.9.0_x64_custom\5.9\msvc2015_64" -opengl es2 -angle -icu -qt-pcre -qt-zlib -qt-freetype -qt-libpng -qt-libjpeg -sql-sqlite -nomake tests -nomake examples -skip qtdoc -openssl -I X:\icu4c\include -L X:\icu4c\lib64 -I X:\OpenSSL-Win64\include -L X:\OpenSSL-Win64\lib

静态版 --> configure -confirm-license -opensource -platform win32-msvc2015 -release -static -mp -prefix "X:\Qt\Qt5.9.0_x64_custom\5.9\msvc2015_64" -opengl es2 -angle -qt-pcre -qt-zlib -qt-freetype -qt-libpng -qt-libjpeg -sql-sqlite -nomake tests -nomake examples -skip qtdoc -openssl -I X:\OpenSSL-Win64\include -L X:\OpenSSL-Win64\lib

编译并将结果拷贝到 X:\Qt\Qt5.9.0_x64_custom 目录下

jom --> jom install 或 nmake --> nmake install

编译 QtWebEngine


Qt WebEngine 只能在带有 x64 位工具链的 64 位 Windows 中进行构建。

若想构建用于 x86 应用程序的 Qt WebEngine,需要采用 Visual Studio 2015 x64 to x86 交叉编译工具链来配置和编译 Qt。 可通过运行 vcvarsall.bat amd64_x86,在命令行中设置此工具链。

注意事项


Qt5.9.0\qtwebengine --> warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失 --> qtcreator --> 工具 --> 选项 --> 文本编辑器 --> 行为 --> UTF-8 BOM: 如果编码是 UTF-8 则添加 --> 打开文件 --> 保存文件

相关工具或软件,请跳转 下载中心

若仍不会编译,可联系 客服 寻求帮助 (或关注乐数软件相关在线资源)。

configure 选项


Qt 5.9.1 在 CMD 窗口, configure -? 输出信息。

X:\Qt\qt-everywhere-opensource-src-5.9.1>configure -help
+ cd qtbase
+ X:\Qt\qt-everywhere-opensource-src-5.9.1\qtbase\configure.bat -top-level -help
Usage:  configure [options] [assignments]
 
Configure understands variable assignments like VAR=value on the command line.
Each uppercased library name (obtainable with -list-libraries) supports the
suffixes _INCDIR, _LIBDIR, _PREFIX (INCDIR=PREFIX/include, LIBDIR=PREFIX/lib),
_LIBS, and - on Windows and Darwin - _LIBS_DEBUG and _LIBS_RELEASE. E.g.,
ICU_PREFIX=/opt/icu42 ICU_LIBS="-licui18n -licuuc -licudata".
 
It is also possible to manipulate any QMAKE_* variable, to amend the values
from the mkspec for the build of Qt itself, e.g., QMAKE_CXXFLAGS+=-g3.
 
Top-level installation directories:
  -prefix <dir> ...... The deployment directory, as seen on the target device.
                       [/usr/local/Qt-$QT_VERSION, $PWD if -developer-build]
  -extprefix <dir> ... The installation directory, as seen on the host machine.
                       [SYSROOT/PREFIX]
  -hostprefix [dir] .. The installation directory for build tools running on
                       the host machine. If [dir] is not given, the current
                       build directory will be used. [EXTPREFIX]
  -external-hostbindir <path> ... Path to Qt tools built for this machine.
                       Use this when -platform does not match the current
                       system, i.e., to make a Canadian Cross Build.
 
Fine tuning of installation directory layout. Note that all directories
except -sysconfdir should be located under -prefix/-hostprefix:
 
  -bindir <dir> ......... Executables [PREFIX/bin]
  -headerdir <dir> ...... Header files [PREFIX/include]
  -libdir <dir> ......... Libraries [PREFIX/lib]
  -archdatadir <dir> .... Arch-dependent data [PREFIX]
  -plugindir <dir> ...... Plugins [ARCHDATADIR/plugins]
  -libexecdir <dir> ..... Helper programs [ARCHDATADIR/bin on Windows,
                          ARCHDATADIR/libexec otherwise]
  -importdir <dir> ...... QML1 imports [ARCHDATADIR/imports]
  -qmldir <dir> ......... QML2 imports [ARCHDATADIR/qml]
  -datadir <dir> ........ Arch-independent data [PREFIX]
  -docdir <dir> ......... Documentation [DATADIR/doc]
  -translationdir <dir> . Translations [DATADIR/translations]
  -sysconfdir <dir> ..... Settings used by Qt programs [PREFIX/etc/xdg]
  -examplesdir <dir> .... Examples [PREFIX/examples]
  -testsdir <dir> ....... Tests [PREFIX/tests]
 
  -hostbindir <dir> ..... Host executables [HOSTPREFIX/bin]
  -hostlibdir <dir> ..... Host libraries [HOSTPREFIX/lib]
  -hostdatadir <dir> .... Data used by qmake [HOSTPREFIX]
 
Conventions for the remaining options: When an option's description is
followed by a list of values in brackets, the interpretation is as follows:
'yes' represents the bare option; all other values are possible prefixes to
the option, e.g., -no-gui. Alternatively, the value can be assigned, e.g.,
--gui=yes. Values are listed in the order they are tried if not specified;
'auto' is a shorthand for 'yes/no'. Solitary 'yes' and 'no' represent binary
options without auto-detection.
 
Configure meta:
 
  -help, -h ............ Display this help screen
  -verbose, -v ......... Print verbose messages during configuration
  -continue ............ Continue configure despite errors
  -redo ................ Re-configure with previously used options.
                         Additional options may be passed, but will not be
                         saved for later use by -redo.
  -recheck ............. Discard cached negative configure test results.
                         Use this after installing missing dependencies.
  -recheck-all ......... Discard all cached configure test results.
 
  -feature-<feature> ... Enable <feature>
  -no-feature-<feature>  Disable <feature> [none]
  -list-features ....... List available features. Note that some features
                         have dedicated command line options as well.
 
  -list-libraries ...... List possible external dependencies.
 
Build options:
 
  -opensource .......... Build the Open-Source Edition of Qt
  -commercial .......... Build the Commercial Edition of Qt
  -confirm-license ..... Automatically acknowledge the license
 
  -release ............. Build Qt with debugging turned off [yes]
  -debug ............... Build Qt with debugging turned on [no]
  -debug-and-release ... Build two versions of Qt, with and without
                         debugging turned on [yes] (Apple and Windows only)
  -optimize-debug ...... Enable debug-friendly optimizations in debug builds
                         [auto] (Not supported with MSVC)
  -optimize-size ....... Optimize release builds for size instead of speed [no]
  -optimized-tools ..... Build optimized host tools even in debug build [no]
  -force-debug-info .... Create symbol files for release builds [no]
  -separate-debug-info . Split off debug information to separate files [no]
  -strip ............... Strip release binaries of unneeded symbols [yes]
  -force-asserts ....... Enable Q_ASSERT even in release builds [no]
  -developer-build ..... Compile and link Qt for developing Qt itself
                         (exports for auto-tests, extra checks, etc.) [no]
 
  -shared .............. Build shared Qt libraries [yes] (no for UIKit)
  -static .............. Build static Qt libraries [no] (yes for UIKit)
  -framework ........... Build Qt framework bundles [yes] (Apple only)
 
  -platform <target> ... Select host mkspec [detected]
  -xplatform <target> .. Select target mkspec when cross-compiling [PLATFORM]
  -device <name> ....... Cross-compile for device <name>
  -device-option <key=value> ... Add option for the device mkspec
 
  -appstore-compliant .. Disable code that is not allowed in platform app stores.
                         This is on by default for platforms which require distribution
                         through an app store by default, in particular Android,
                         iOS, tvOS, watchOS, and Universal Windows Platform. [auto]
 
  -qtnamespace <name> .. Wrap all Qt library code in 'namespace <name> {...}'.
  -qtlibinfix <infix> .. Rename all libQt5*.so to libQt5*<infix>.so.
 
  -testcocoon .......... Instrument with the TestCocoon code coverage tool [no]
  -gcov ................ Instrument with the GCov code coverage tool [no]
  -sanitize {address|thread|memory|undefined}
                         Instrument with the specified compiler sanitizer.
 
  -c++std <edition> .... Select C++ standard <edition> [c++1z/c++14/c++11]
                         (Not supported with MSVC)
 
  -sse2 ................ Use SSE2 instructions [auto]
  -sse3/-ssse3/-sse4.1/-sse4.2/-avx/-avx2/-avx512
                         Enable use of particular x86 instructions [auto]
                         Enabled ones are still subject to runtime detection.
  -mips_dsp/-mips_dspr2  Use MIPS DSP/rev2 instructions [auto]
 
  -qreal <type> ........ typedef qreal to the specified type. [double]
                         Note: this affects binary compatibility.
 
  -R <string> .......... Add an explicit runtime library path to the Qt
                         libraries. Supports paths relative to LIBDIR.
  -rpath ............... Link Qt libraries and executables using the library
                         install path as a runtime library path. Similar to
                         -R LIBDIR. On Apple platforms, disabling this implies
                         using absolute install names (based in LIBDIR) for
                         dynamic libraries and frameworks. [auto]
 
  -reduce-exports ...... Reduce amount of exported symbols [auto]
  -reduce-relocations .. Reduce amount of relocations [auto] (Unix only)
 
  -plugin-manifests .... Embed manifests into plugins [no] (Windows only)
  -static-runtime ...... With -static, use static runtime [no] (Windows only)
 
  -pch ................. Use precompiled headers [auto]
  -ltcg ................ Use Link Time Code Generation [no]
  -use-gold-linker ..... Use the GNU gold linker [auto]
  -incredibuild-xge .... Use the IncrediBuild XGE [no] (Windows only)
  -make-tool <tool> .... Use <tool> to build qmake [nmake] (Windows only)
  -mp .................. Use multiple processors for compilation (MSVC only)
 
  -warnings-are-errors . Treat warnings as errors [no; yes if -developer-build]
  -silent .............. Reduce the build output so that warnings and errors
                         can be seen more easily
 
Build environment:
 
  -sysroot <dir> ....... Set <dir> as the target sysroot
  -gcc-sysroot ......... With -sysroot, pass --sysroot to the compiler [yes]
 
  -pkg-config .......... Use pkg-config [auto] (Unix only)
 
  -D <string> .......... Pass additional preprocessor define
  -I <string> .......... Pass additional include path
  -L <string> .......... Pass additional library path
  -F <string> .......... Pass additional framework path (Apple only)
 
  -sdk <sdk> ........... Build Qt using Apple provided SDK <sdk>. The argument
                         should be one of the available SDKs as listed by
                         'xcodebuild -showsdks'.
                         Note that the argument applies only to Qt libraries
                         and applications built using the target mkspec - not
                         host tools such as qmake, moc, rcc, etc.
 
  -android-sdk path .... Set Android SDK root path [$ANDROID_SDK_ROOT]
  -android-ndk path .... Set Android NDK root path [$ANDROID_NDK_ROOT]
  -android-ndk-platform  Set Android platform
  -android-ndk-host .... Set Android NDK host (linux-x86, linux-x86_64, etc.)
                         [$ANDROID_NDK_HOST]
  -android-arch ........ Set Android architecture (armeabi, armeabi-v7a,
                         arm64-v8a, x86, x86_64, mips, mips64)
  -android-toolchain-version ... Set Android toolchain version
  -android-style-assets  Automatically extract style assets from the device at
                         run time. This option makes the Android style behave
                         correctly, but also makes the Android platform plugin
                         incompatible with the LGPL2.1. [yes]
 
Component selection:
 
  -skip <repo> ......... Exclude an entire repository from the build.
  -make <part> ......... Add <part> to the list of parts to be built.
                         Specifying this option clears the default list first.
                         [libs and examples, also tools if not cross-building,
                         also tests if -developer-build]
  -nomake <part> ....... Exclude <part> from the list of parts to be built.
  -compile-examples .... When unset, install only the sources of examples [yes]
  -gui ................. Build the Qt GUI module and dependencies [yes]
  -widgets ............. Build the Qt Widgets module and dependencies [yes]
  -no-dbus ............. Do not build the Qt D-Bus module
                         [default on Android and Windows]
  -dbus-linked ......... Build Qt D-Bus and link to libdbus-1 [auto]
  -dbus-runtime ........ Build Qt D-Bus and dynamically load libdbus-1 [no]
  -accessibility ....... Enable accessibility support [yes]
                         Note: Disabling accessibility is not recommended.
  -qml-debug ........... Enable QML debugging support [yes]
 
Qt comes with bundled copies of some 3rd party libraries. These are used
by default if auto-detection of the respective system library fails.
 
Core options:
 
  -doubleconversion .... Select used double conversion library [system/Qt/no]
                         No implies use of sscanf_l and snprintf_l (imprecise).
  -glib ................ Enable Glib support [no; auto on Unix]
  -eventfd ............. Enable eventfd support
  -inotify ............. Enable inotify support
  -iconv ............... Enable iconv(3) support [posix/sun/gnu/no] (Unix only)
  -icu ................. Enable ICU support [auto]
  -pcre ................ Select used libpcre2 [system/qt]
  -pps ................. Enable PPS support [auto] (QNX only)
  -zlib ................ Select used zlib [system/qt]
 
  Logging backends:
    -journald .......... Enable journald support [no] (Unix only)
    -syslog ............ Enable syslog support [no] (Unix only)
    -slog2 ............. Enable slog2 support [auto] (QNX only)
 
Network options:
 
  -ssl ................. Enable either SSL support method [auto]
  -no-openssl .......... Do not use OpenSSL [default on Apple and WinRT]
  -openssl-linked ...... Use OpenSSL and link to libssl [no]
  -openssl-runtime ..... Use OpenSSL and dynamically load libssl [auto]
  -securetransport ..... Use SecureTransport [auto] (Apple only)
 
  -sctp ................ Enable SCTP support [no]
 
  -libproxy ............ Enable use of libproxy [no]
  -system-proxies ...... Use system network proxies by default [yes]
 
Gui, printing, widget options:
 
  -cups ................ Enable CUPS support [auto] (Unix only)
 
  -fontconfig .......... Enable Fontconfig support [auto] (Unix only)
  -freetype ............ Select used FreeType [system/Qt/no]
  -harfbuzz ............ Select used HarfBuzz-NG [system/Qt/no]
                         (Not auto-detected on Apple and Windows)
 
  -gtk ................. Enable GTK platform theme support [auto]
 
  -lgmon ............... Enable lgmon support [auto] (QNX only)
 
  -no-opengl ........... Disable OpenGL support
  -opengl <api> ........ Enable OpenGL support. Supported APIs:
                         es2 (default on Windows), desktop (default on Unix),
                         dynamic (Windows only)
  -opengles3 ........... Enable OpenGL ES 3.x support instead of ES 2.x [auto]
  -angle ............... Use bundled ANGLE to support OpenGL ES 2.0 [auto]
                         (Windows only)
  -combined-angle-lib .. Merge LibEGL and LibGLESv2 into LibANGLE (Windows only)
 
  -qpa <name> .......... Select default QPA backend (e.g., xcb, cocoa, windows)
  -xcb-xlib............. Enable Xcb-Xlib support [auto]
 
  Platform backends:
    -direct2d .......... Enable Direct2D support [auto] (Windows only)
    -directfb .......... Enable DirectFB support [no] (Unix only)
    -eglfs ............. Enable EGLFS support [auto; no on Android and Windows]
    -gbm ............... Enable backends for GBM [auto] (Linux only)
    -kms ............... Enable backends for KMS [auto] (Linux only)
    -linuxfb ........... Enable Linux Framebuffer support [auto] (Linux only)
    -mirclient ......... Enable Mir client support [no] (Linux only)
    -xcb ............... Select used xcb-* libraries [system/Qt/no]
                         (-qt-xcb still uses system version of libxcb itself)
 
  Input backends:
    -evdev ............. Enable evdev support [auto]
    -imf ............... Enable IMF support [auto] (QNX only)
    -libinput .......... Enable libinput support [auto]
    -mtdev ............. Enable mtdev support [auto]
    -tslib ............. Enable tslib support [auto]
    -xinput2 ........... Enable XInput2 support [auto]
    -xkbcommon-x11 ..... Select xkbcommon used in combination with xcb
                         [system/Qt/no]
    -xkb-config-root <dir> ... With -qt-xkbcommon-x11, set default XKB config
                               root <dir> [detect]
    -xkbcommon-evdev ... Enable X-less xkbcommon in combination with libinput
                         [auto]
 
  Image formats:
    -gif ............... Enable reading support for GIF [auto]
    -ico ............... Enable support for ICO [yes]
    -libpng ............ Select used libpng [system/Qt/no]
    -libjpeg ........... Select used libjpeg [system/Qt/no]
 
Database options:
 
  -sql-<driver> ........ Enable SQL <driver> plugin. Supported drivers:
                         db2 ibase mysql oci odbc psql sqlite2 sqlite tds
                         [all auto]
  -sqlite .............. Select used sqlite3 [system/qt]
 
Qt3D options:
 
  -assimp .............. Select used assimp library [system/Qt/no]
  -qt3d-profile-jobs ... Enable jobs profiling [no]
  -qt3d-profile-gl ..... Enable OpenGL profiling [no]
 
Multimedia options:
 
  -pulseaudio .......... Enable PulseAudio support [auto] (Unix only)
  -alsa ................ Enable ALSA support [auto] (Unix only)
  -no-gstreamer ........ Disable support for GStreamer
  -gstreamer [version] . Enable GStreamer support [auto]
                         With no parameter, 1.0 is tried first, then 0.10.
  -mediaplayer-backend <name> ... Select media player backend (Windows only)
                                  Supported backends: directshow (default), wmf
 
X:\Qt\qt-everywhere-opensource-src-5.9.1>
					

版权声明: 本文为独家原创稿件,版权归 乐数软件 ,未经许可不得转载。