Qt: Static Builds Under Windows

23 de Abril, 2020

Informática, Open-Source, Programação, Software, Windows

Qt: Static Builds Under Windows

23 de Abril, 2020 | Informática, Open-Source, Programação, Software, Windows

Qt is a cross-platform software development framework for C++ used to build software across all major industries. If you work at company that uses Qt you may have wondered how you can pack all dependencies of your application in a single executable. Yes, that is possible but only with static builds. Let me show you how to do it.

Disclaimer: According to the Qt license you may not deploy static builds with the open-source license. Deploying static builds is only allowed for companies who purchase Qt.

Does this image look familiar to you?

Yeah Qt has so many dependencies… with static builds Qt can generate a single executable file so, no more missing dependencies and no need to use external tools such as Qt’s windeployqt or Dependency Walker.

Unfortunately the only way to do static builds is to compile Qt itself statically first. I’ll show you how to do it and how to setup Qt Creator to do both shared and static builds:

Requirements

  • A computer to develop Qt applications with Qt and Qt Creator already installed;
  • A Windows 10 (64 bit) virtual machine to build a static version of Qt;
  • Qt source code: https://www.qt.io/offline-installers > Offline Installers > Source packages > For Windows users as a single zip file.
  • mingw-w64: https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe/download
  • Perl (64 bit): http://strawberryperl.com/
  • Ruby (64 bit): https://rubyinstaller.org/downloads/
  • Python 2.7: https://www.python.org/downloads/

Setup

In your virtual machine start by unpacking the Qt Source code to the root of the system, for instance C:\qt-everywhere-src-5.14.2. Now install Perl, Python, Ruby and then MinGW with the following settings:

  • Architecture: x86_64
  • Threads: win32
  • Installation path: C:\Qt-Static\mingw-w64

Make sure Perl, Python, Ruby and MinGW are in your system path and MinGW is the first one. This will avoid issues with the build in C compiler of Strawberry Perl. Example:

Compiling Qt Statically

Now that we have all dependencies open command prompt as admin and:

cd C:\qt-everywhere-src-5.14.2
configure -debug-and-release -static -opengl desktop -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -nomake examples -nomake tests

This will configure the source code for a static build capable of debug and release configurations. I’ve added OpenGL for desktop applications and most of the commonly used libraries. I’ve also excluded examples and tests – those are already available in our development machine. Now build and install Qt with:

mingw32-make
mingw32-make install

Qt will be installed to C:\Qt\Qt-5.14.2, now you may copy that directory over to our final destination C:\Qt-Static\Qt-5.14.2.

Configure Your Development Machine

Start by copying the static build folder C:\Qt-Static from the virtual machine to the same path in your development machine. Open Qt Creator and go to Tools > Options > Kits.

Now under Compilers add the following:

  • C Compiler:
    • Name: MinGW Static
    • Path: C:\Qt-Static\mingw-w64\mingw64\bin\gcc.exe
  • C++ Compiler:
    • Name: MinGW++ Static
    • Path: C:\Qt-Static\mingw-w64\mingw64\bin\g++.exe

Under Qt Versions add a new version as:

  • Version name: Qt-Static %{Qt:Version} (Qt-5.14.2)
  • qmake location: C:\Qt-Static\Qt-5.14.2\bin\qmake.exe

Now go to Kits and add a Kit that uses the static Qt version and the compiler we’ve added before:

Project Configuration

In order to do static builds you still need to add the following to your project’s .pro file:

# Static Builds
QTPREFIX=$$[QT_INSTALL_PREFIX]
equals(QTPREFIX, "C:/Qt-Static/Qt-5.14.2"){
    message("--STATIC BUILD--")
    CONFIG += qt static
    QMAKE_LFLAGS += -static-libgcc -static-libstdc++
} else {
    message("--NON-STATIC BUILD--")
}

This configuration will detect whenever you’re building with the static Qt and make sure the proper flags are added.

Congratulations, you’re ready for your first static build.