现在Qt官方提供的mingw编译器的预编译版本都是32bit的,如果想使用64bit的话需要自己手动进行编译。
自己编译的话只需要下载mingw-w64编译器和Qt的源码,之前在官方Wiki上看到还需要Perl,实际测试发现没有Perl也可以正常编译。
mingw-w64直接去mingw-w64官网下载就可以了,选择MingW-W64-builds下载。安装时记得架构记得选择x86_64,版本的话选择最新的就可以了,我编译时使用的是8.1.0没有问题。
Qt源码的话也去Qt官网下载,5.9.6版本的下载地址为http://download.qt.io/archive/qt/5.9/5.9.6/single/qt-everywhere-opensource-src-5.9.6.zip,其他版本的可以到http://download.qt.io/archive/qt/ 到对应目录下载,如果下载预编译版本不想注册的话也可以直接从这里下载安装包。
下载好Qt安装包后直接进行解压,解压完成后需要注意默认解压出的路径比较长比如E:\qt-everywhere-opensource-src-5.9.6\qt-everywhere-opensource-src-5.9.6,mingw存在bug,太长的路径编译会报错,找不到相应的头文件’ No such file or directory #include “… qfreetypefontdatabase_p.h”‘。
解压完成后,在源文件夹目录新建以下bat脚本,QtPrefixDir为执行make install命令时安装的目录、MingwDir为安装的mingw-w64的路径。
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 |
@echo off rem ====================CONFIGURE BEFORE RUN SCRIPT!!====================== set QtSrcDir=%CD% set QtPrefixDir=C:\Qt\5.9.1-x64 set MingwDir=C:\mingw-w64\x86_64-7.1.0-posix-seh-rt_v5-rev0\mingw64\ set BuildType=-debug-and-release set QtSPEC=win32-g++ set LANG = en rem ======================================================================= PATH = %MingwDir%\bin;%MingwDir%\opt\bin;%SystemRoot%\system32;%SystemRoot% set QT_INSTALL_PREFIX = %QtStaticDir% set OldDir=%CD% cd %QtSrcDir% cmd /C "configure.bat -shared %BuildType% -platform %QtSPEC% -prefix %QtPrefixDir% -opensource -confirm-license -c++std c++14 -nomake examples -skip wayland -skip purchasing -skip serialbus -skip qtserialport -skip script -skip scxml -skip speech -skip location -no-opengl -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -no-openssl" if ERRORLEVEL 1 goto :error mingw32-make -r -k -s -j4 if ERRORLEVEL 1 goto :error mingw32-make -k install if ERRORLEVEL 1 goto :error set FILE_TO_PATCH=%QtPrefixDir%\mkspecs\win32-g++\qmake.conf echo.>>%FILE_TO_PATCH% echo CONFIG += static>>%FILE_TO_PATCH% echo ============BUILT!============ goto exitX :error echo ============ERROR!============ :exitX pause cd %OldDir% |
以上的编译的是动态链接库,如果要静态链接库的话可以用下面的脚本。
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 |
@echo off rem ====================CONFIGURE BEFORE RUN SCRIPT!!====================== set QtSrcDir=%CD%\qt-everywhere-opensource-src-5.9.1 set QtPrefixDir=C:\Qt\5.9.1-x64-static set MingwDir=C:\mingw-w64\x86_64-7.1.0-posix-seh-rt_v5-rev0\mingw64\ set BuildType=-release set QtSPEC=win32-g++ set LANG = en rem ======================================================================= PATH = %MingwDir%\bin;%MingwDir%\opt\bin;%SystemRoot%\system32;%SystemRoot% set FILE_TO_PATCH=%QtSrcDir%\qtbase\mkspecs\win32-g++\qmake.conf echo %FILE_TO_PATCH% if exist %FILE_TO_PATCH%.patched goto skipPatch type %FILE_TO_PATCH%>%FILE_TO_PATCH%.patched echo.>>%FILE_TO_PATCH% echo QMAKE_LFLAGS += -static -static-libgcc>>%FILE_TO_PATCH% echo QMAKE_CFLAGS_RELEASE -= -O2>>%FILE_TO_PATCH% echo QMAKE_CFLAGS_RELEASE += -Os -momit-leaf-frame-pointer>>%FILE_TO_PATCH% echo DEFINES += QT_STATIC_BUILD>>%FILE_TO_PATCH% :skipPatch set QT_INSTALL_PREFIX = %QtStaticDir% set OldDir=%CD% cd %QtSrcDir% cmd /C "configure.bat -shared %BuildType% -platform %QtSPEC% -prefix %QtPrefixDir% -opensource -confirm-license -c++std c++14 -nomake examples -skip wayland -skip purchasing -skip serialbus -skip qtserialport -skip script -skip scxml -skip speech -skip location -no-opengl -qt-zlib -qt-pcre -qt-libpng -no-libjpeg -qt-freetype -no-openssl" if ERRORLEVEL 1 goto :error mingw32-make -r -k -s -j4 if ERRORLEVEL 1 goto :error mingw32-make -k install if ERRORLEVEL 1 goto :error set FILE_TO_PATCH=%QtPrefixDir%\mkspecs\win32-g++\qmake.conf echo.>>%FILE_TO_PATCH% echo CONFIG += static>>%FILE_TO_PATCH% echo ============BUILT!============ goto exitX :error echo ============ERROR!============ :exitX pause cd %OldDir% |
参考资料:
https://wohlsoft.ru/pgewiki/Building_Qt_on_MinGW-w64
https://bugreports.qt.io/browse/QTBUG-64298
哥哥真棒