静态编译 Qt 5.6

源码下载

  • Qt git地址:git://code.qt.io/qt/qt5.git
  • 推荐用 SourceTree 客户端来克隆源码

准备工作

  • 系统 Windows 7 或者 Windows 10
  • 编译器 Visual Studio 2013 Update 5 或者 Visual Studio 2015 Update 1
  • 安装ActivePerl
  • 安装Python
  • 静态编译ICU库
    • ICU库需要把DebugRelease版本都编出来
  • 静态编译openssl
  • 编译webkit需要的环境
  • 最重要的准备工作
    • 关闭360等安全卫士,杀毒软件,编译过程中360会弹框拦截
  • 使用 vs2013 编译的时候,有可能会遇到以下错误

    1
    Error 2 error C2440: 'default argument' : cannot convert from 'const wchar_t [1]' to 'const BSTR'   (src\GAudioOutput.cc) C:\Program Files (x86)\Windows Kits\8.1\Include\um\sapi.h 16917 1 qgroundcontrol

    遇到这个问题,需要修改 qtbase\mkspecs\common\msvc-base.conf 文件,将下面这段删除,如图

  • 编译前,最好将 git python perl ruby等相关的路径从系统的PATH环境变量中删除

编译错误

  • 编译过程中会遇到以下错误

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    libeay32.lib(rand_win.obj) : error LNK2019: 无法解析的外部符号 __imp__BitBlt@36,该符号在函数 _readscreen 中被引用
    libeay32.lib(rand_win.obj) : error LNK2019: 无法解析的外部符号 __imp__CreateCompatibleBitmap@12,该符号在函数 _readscreen 中被引用
    libeay32.lib(rand_win.obj) : error LNK2019: 无法解析的外部符号 __imp__CreateCompatibleDC@4,该符号在函数 _readscreen 中被引用
    libeay32.lib(rand_win.obj) : error LNK2019: 无法解析的外部符号 __imp__CreateDCA@16,该符号在函数 _readscreen 中被引用
    libeay32.lib(rand_win.obj) : error LNK2019: 无法解析的外部符号 __imp__DeleteDC@4,该符号在函数 _readscreen 中被引用
    libeay32.lib(rand_win.obj) : error LNK2019: 无法解析的外部符号 __imp__DeleteObject@4,该符号在函数 _readscreen 中被引用
    libeay32.lib(rand_win.obj) : error LNK2019: 无法解析的外部符号 __imp__GetBitmapBits@12,该符号在函数 _readscreen 中被引用
    libeay32.lib(rand_win.obj) : error LNK2019: 无法解析的外部符号 __imp__GetDeviceCaps@8,该符号在函数 _readscreen 中被引用
    libeay32.lib(rand_win.obj) : error LNK2019: 无法解析的外部符号 __imp__SelectObject@8,该符号在函数 _readscreen 中被引用
    libeay32.lib(rand_win.obj) : error LNK2019: 无法解析的外部符号 __imp__GetObjectA@12,该符号在函数 _readscreen 中被引用

    这个需要在配置参数中增加一个链接库

    1
    -L "C:/Program Files (x86)/Microsoft SDKs/Windows/v7.1A/Lib" -l Gdi32

编译

  • 新建一个 environment.bat,用来执行环境变量的设置,代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    @echo off

    rem 设置vs安装路径
    set MSVC_PATH=D:\Programs\Microsoft Visual Studio 12.0

    rem 设置icu源码路径,在编译icu时用到,编译qt用不到源码
    set ICU_SRC_PATH=D:\Qt\static_build\icu-source\source

    rem 设置icu编译release安装的路径,在编译icu时用到,编译qt用不到这个格式的路径,注意安装路径格式,是linux风格
    set ICU_INSTALL_PATH=/cygdrive/D/Qt/static_build/icu

    rem 设置icu编译debug安装的路径,在编译icu时用到,编译qt用不到这个格式的路径,注意安装路径格式,是linux风格
    set ICU_INSTALL_PATH_DEBUG=/cygdrive/D/Qt/static_build/icu-debug

    rem 设置CYG依赖,在编译icu时用到,编译qt用不到
    set CYG_WIN_PATH=D:\Programs\Dev\cygwin64\bin

    rem 设置ICU编译release安装的路径,编译Qt会用到,注意格式,是Windows风格
    set ICU_INSTALL_PATH_WIN=D:\Qt\static_build\icu

    rem 设置ICU编译debug安装的路径,编译Qt会用到,注意格式,是Windows风格
    set ICU_INSTALL_PATH_WIN_DEBUG=D:\Qt\static_build\icu-debug

    rem 设置openssl源码路径,在编译openssl时用到,编译qt用不到源码
    set OPENSSL_SRC_PATH=D:\Qt\static_build\openssl-1.0.1p

    rem 设置openssl的最终安装路径,编译qt时会用到
    set OPENSSL_INSTALL_PATH=D:\Qt\static_build\openssl

    rem 设置编译Qt的编译器配置文件名
    set QMAKESPEC=win32-msvc2013

    rem 设置qt的源码路径
    set QT5_SRC_PATH=D:\Qt\static_build\qt5

    rem 设置qt编译后的安装路径
    set QT5_INSTALL_PATH=D:\Qt\qt_5.6.0_release_2013_static

    rem 设置git安装路径
    set GIT_PATH=C:\Program Files\Git

    rem 设置Perl的bin路径
    set PERL_PATH=D:\Programs\Dev\Perl\bin

    rem 设置python安装路径
    set PYTHON_PATH=D:\Programs\Dev\Python27

    rem qtwebkit依赖环境
    set WIN_FLEX_BISION_PATH=D:\Programs\Dev\win_flex_bison-latest
    set SQLITE3SRCDIR=D:\Qt\3rd\sqlite-amalgamation-3090100
    set RUBY_PATH=D:\Programs\Dev\Ruby22\bin

    rem 执行MSVC环境设置
    call "%MSVC_PATH%\VC\vcvarsall.bat" x86
  • 新建一个 build_qt.bat,用来执行qt的编译,代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    @echo off

    call environment.bat

    set PATH=%QT5_SRC_PATH%\qtbase\bin;%QT5_SRC_PATH%\qtbase\lib;%QT5_SRC_PATH%\gnuwin32\bin;%GIT_PATH%\bin;%PATH%
    set PATH=%QT5_SRC_PATH%\qtrepotools\bin;%PATH%
    set PATH=%PATH%;%PERL_PATH%;%PYTHON_PATH%

    set LIB=%LIB%;%ICU_INSTALL_PATH_WIN%\lib;%ICU_INSTALL_PATH_WIN_DEBUG%\lib;%OPENSSL_INSTALL_PATH%\lib
    set INCLUDE=%INCLUDE%;%ICU_INSTALL_PATH_WIN%\include;%OPENSSL_INSTALL_PATH%\include
    set PATH=%PATH%;%ICU_INSTALL_PATH_WIN%\bin;%OPENSSL_INSTALL_PATH%\bin;%ICU_INSTALL_PATH_WIN%\lib;%ICU_INSTALL_PATH_WIN_DEBUG%\lib

    cd /D %QT5_SRC_PATH%

    call %QT5_SRC_PATH%\configure -prefix %QT5_INSTALL_PATH% -debug-and-release -force-debug-info -opensource -confirm-license -static -static-runtime -opengl dynamic -mp -icu -openssl-linked -nomake examples -nomake tests -no-compile-examples -target xp -no-directwrite -L "C:/Program Files (x86)/Microsoft SDKs/Windows/v7.1A/Lib" -l Gdi32

    nmake

    nmake install

    rem 复制openssl和icu到qt安装目录中
    xcopy /y /k "%ICU_INSTALL_PATH_WIN%\lib\sicudt.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%ICU_INSTALL_PATH_WIN%\lib\sicuin.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%ICU_INSTALL_PATH_WIN%\lib\sicuuc.lib" "%QT5_INSTALL_PATH%\lib\"

    xcopy /y /k "%ICU_INSTALL_PATH_WIN_DEBUG%\lib\sicudtd.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%ICU_INSTALL_PATH_WIN_DEBUG%\lib\sicuind.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%ICU_INSTALL_PATH_WIN_DEBUG%\lib\sicuucd.lib" "%QT5_INSTALL_PATH%\lib\"

    xcopy /y /k "%OPENSSL_INSTALL_PATH%\lib\libeay32.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%OPENSSL_INSTALL_PATH%\lib\ssleay32.lib" "%QT5_INSTALL_PATH%\lib\"

    pause

  • 双击执行 build_qt.bat,整个编译过程可能需要4、5个小时

  • 新建一个 build_qtwebkit.bat,用来执行qtwebkit的编译,代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    @echo off

    call environment.bat

    set LIB=%LIB%;%ICU_INSTALL_PATH_WIN%\lib;%ICU_INSTALL_PATH_WIN_DEBUG%\lib;%OPENSSL_INSTALL_PATH%\lib;%QT5_INSTALL_PATH%\lib
    set INCLUDE=%INCLUDE%;%ICU_INSTALL_PATH_WIN%\include;%OPENSSL_INSTALL_PATH%\include;%QT5_INSTALL_PATH%\include
    set PATH=%QT5_INSTALL_PATH%\bin;%WIN_FLEX_BISION_PATH%;%ICU_INSTALL_PATH_WIN%\bin;%OPENSSL_INSTALL_PATH%\bin;%ICU_INSTALL_PATH_WIN%\lib;%ICU_INSTALL_PATH_WIN_DEBUG%\lib;%QT5_SRC_PATH%\gnuwin32\bin;%PERL_PATH%;%PYTHON_PATH%;%RUBY_PATH%;%PATH%

    cd /D %QT5_SRC_PATH%

    %PERL_PATH%\perl %QT5_SRC_PATH%\qtwebkit\Tools\Scripts\build-webkit --qt > build_qtwebkit.log

    rem 复制qtwebkit静态库到qt安装目录中
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\bin\QtTestBrowser.exe" "%QT5_INSTALL_PATH%\bin\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\bin\QtTestBrowser.pdb" "%QT5_INSTALL_PATH%\bin\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\bin\jsc.exe" "%QT5_INSTALL_PATH%\bin\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\bin\jsc.pdb" "%QT5_INSTALL_PATH%\bin\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\bin\ImageDiff.exe" "%QT5_INSTALL_PATH%\bin\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\bin\ImageDiff.pdb" "%QT5_INSTALL_PATH%\bin\"

    xcopy /s /e /h /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\imports\*.*" "%QT5_INSTALL_PATH%\imports\"

    xcopy /s /e /h /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\include\*.*" "%QT5_INSTALL_PATH%\include\"

    xcopy /s /e /h /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\lib\*.*" "%QT5_INSTALL_PATH%\lib\"

    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\JavaScriptCore\release\JavaScriptCore.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\JavaScriptCore\release\JavaScriptCore.pdb" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\JavaScriptCore\debug\JavaScriptCored.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\JavaScriptCore\debug\JavaScriptCored.pdb" "%QT5_INSTALL_PATH%\lib\"

    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\ThirdParty\ANGLE\release\ANGLE.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\ThirdParty\ANGLE\release\ANGLE.pdb" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\ThirdParty\ANGLE\debug\ANGLEd.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\ThirdParty\ANGLE\debug\ANGLEd.pdb" "%QT5_INSTALL_PATH%\lib\"

    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\ThirdParty\leveldb\release\leveldb.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\ThirdParty\leveldb\release\leveldb.pdb" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\ThirdParty\leveldb\debug\leveldbd.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\ThirdParty\leveldb\debug\leveldbd.pdb" "%QT5_INSTALL_PATH%\lib\"

    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebCore\release\WebCore.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebCore\release\WebCore.pdb" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebCore\debug\WebCored.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebCore\debug\WebCored.pdb" "%QT5_INSTALL_PATH%\lib\"

    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebKit\release\WebKit1.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebKit\release\WebKit1.pdb" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebKit\debug\WebKit1d.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebKit\debug\WebKit1d.pdb" "%QT5_INSTALL_PATH%\lib\"

    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebKit2\release\WebKit2.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebKit2\release\WebKit2.pdb" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebKit2\debug\WebKit2d.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WebKit2\debug\WebKit2d.pdb" "%QT5_INSTALL_PATH%\lib\"

    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WTF\release\WTF.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WTF\release\WTF.pdb" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WTF\debug\WTFd.lib" "%QT5_INSTALL_PATH%\lib\"
    xcopy /y /k "%QT5_SRC_PATH%\qtwebkit\WebKitBuild\Release\Source\WTF\debug\WTFd.pdb" "%QT5_INSTALL_PATH%\lib\"

    pause

  • 如果要重新编译,需要清除之前编译的临时文件,可以新建一个 clean_qt.bat 来执行清理工作,代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @echo off

    call environment.bat

    set PATH=%QT5_SRC_PATH%\qtbase\bin;%QT5_SRC_PATH%\qtbase\lib;%QT5_SRC_PATH%\gnuwin32\bin;%GIT_PATH%\bin;%PATH%
    set PATH=%QT5_SRC_PATH%\qtrepotools\bin;%PATH%
    set PATH=%PATH%;%PERL_PATH%;%PYTHON_PATH%

    cd /D %QT5_SRC_PATH%

    git submodule foreach --recursive "git clean -dfx"

    pause

参考文章

编译 Qt 5.6