Building Qt on MinGW-w64

From PGE Wiki
Jump to: navigation, search

This article explain you how to prepare a Qt build environment with using of MinGW-w64 toolchain on Windows operating system. Reason of creating this article is that official MinGW assemblies of Qt are 32-bit only. MinGW-w64 gives you able to have native 64-bit builds with MinGW without usage of Visual Studio.

Prepare MinGW-w64

At first, you need to install MinGW-w64 itself!

Download MinGW-w64 Windows build

Download MinGW-w64 installer

Open official download page: http://mingw-w64.org/doku.php/download and find the "Mingw-builds", and then open "Installation: Sourceforge" link to download the installer.

Installing MinGW-w64 assemblies

  • After starting installer, you will see the greeting page, just press "next".
  • On the settings page choose:
    • Version: Choose the latest (7.1.0 on moment of writing this article)
    • Architecture: i686 for 32-bit builds and x86_64 for 64-bit builds
    • Threads: POSIX (suggested) threads are required for std::thread, std::mutex and std::future. Works slower since it's a proxy over Win32 threads. Win32 threads model will work faster, but to use std::thread, std::mutex and std::future you will need to use extra headers for them.
    • Exception:
      • DWARF (32-bit only, suggested for i686) can cause executables to be slightly bloated because large call stack unwinding tables have to be included in th executables.
      • A method based on setjmp/longjmp (SJLJ). SJLJ-based EH is much slower than DW2 EH (penalising even normal execution when no exceptions are thrown), but can work across code that has not been compiled with GCC or that does not have call-stack unwinding information.
      • Structured Exception Handling (SEH). (suggested for x86_64) Windows uses its own exception handling mechanism known as Structured Exception Handling (SEH).
    • Revision: Usually has 0 only.
  • Choose the installation path: Suggested to don't install into "Program files" and install just into root of any drive (for example, C:\mingw-w64\i686-7.1.0-posix-dwarf-rt_v5-rev0)
  • Wait when installer will download and install your file.

To have able to build for multiple architectures please run installer again, but choose different architecture than installed.

Preparing Qt sources

At first, download the pack of Qt source code here: http://download.qt.io/official_releases/qt/5.9/5.9.1/single/qt-everywhere-opensource-src-5.9.1.zip (the link is points on Qt 5.9.1 which is latest on moment of creating this article. To get newer version you can browse the archive (you will need to open /X.X/X.X.X/single/ folder and take the packed archive) )

When you have to download the archive, please unpack it into any ASCII-only path (for example, into C:\Qt\QtSrc\).

Run build of Qt

Then inside path where you have unpacked create the .bat script with content below. Then find "CONFIGURE BEFORE RUN SCRIPT!!" section and change values of those variables into actual:

  • QtSrcDir must point the actual folder with source code of Qt you have unpacked
  • QtPrefixDir must point the output folder where built Qt assembly will be saved
  • MingwDir must point the MinGW assembly (32-bit or 64-bit) you want to use for Qt building
  • BuildType can contain "-release" for release-only build or "-debug-and-release" to have both debug and release builds

Script for building of 64-bit Shared debug-and-release set

@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
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 -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%


Script for building of 64-bit Static Release set (debug is not recommended for static builds)

@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%

Build and install

When you done everything, run your build script and wait 1.5 ~ 3 hours (dependent to your CPU power) until everything will be built.

Usage

When script will finish it's work, you will have to use the bin/qmake.exe from installation target directory to configure your qt projects to build with static Qt version. You can plug it into Qt Creator with adding reference to bin/qmake.exe from the "Settings" / "Building and debug" tab.