In my view, the cross-platform power of Qt is not fully realized if you need a different build host for every target platform. It’s only natural to want to have one build machine that can deliver multiple targets. And if we’re talking about a CI server for a team to use in a cross-platform application development, then it’s a very worthwhile investment.
Accordingly, the aim of this article is to document how to create a cross-compilation environment that allows you to build Windows applications on a Linux host. Specifically, Qt-based applications.
When it comes to this topic, the internet is populated with naysayers. They will tell you that it’s too hard to setup, that it will take you days (if ever) to setup, that a Windows virtual box is easier, so on and so forth. I can only think such people have never done it, because it really isn’t that hard.
The notes here have been prepared using Qt 5.11.1 (Open Source) and Ubuntu 18.04 LTS 64-bit, as a working example.
The cornerstone of this exercise is MinGW. This is the cross-platform compiler you will need to use instead of the vanilla GCC in your distribution.
sudo apt install mingw-w64
You need to download the Qt source code from https://download.qt.io/official_releases/qt/.
You have two major approaches:
- Download the base source package ( https://download.qt.io/official_releases/qt/5.11/5.11.1/single/ ) and desired individual modules ( https://download.qt.io/official_releases/qt/5.11/5.11.1/submodules/ ).
- Include the source when you install Qt.
In my case, I took the second option, and installed Qt as follows:
chmod +x qt-opensource-linux-x64-5.11.1.run ./qt-opensource-linux-x64-5.11.1.run
Depending on what optional Qt modules you want to include, your prerequisites will likely be different. For example, if you want to build Qt WebEngine, you need flex, bison and more. In my case, the only sub-modules I wanted to add to qtbase were qtserialport, qtsvg and qttools. Hence the only prerequisites were:
sudo apt install build-essential libgl1-mesa-dev python
The Qt build then needs to be configured for cross-compilation using MinGW. The following command configures for creation of a 64-bit Qt development kit. You can instead use i686-w64-mingw32- for 32-bit.
The -prefix clauses specifies the destination directory for the resultant Qt development kit.
As I installed the entire Qt source, and only wanted to build a handful of optional sub-modules, I needed many -skip clauses. YMMV; if you download only the source of sub-modules you want to build, you won’t need any -skip clauses. If the size of the build is not reduced one way or another, the build time will be very much greater, as will be the number of prerequisite packages.
./configure -xplatform win32-g++ -device-option CROSS_COMPILE=/usr/bin/x86_64-w64-mingw32- -prefix /home/foobar/Qt5.11.1/5.11.1/mingw_64 -opensource -no-compile-examples -opengl desktop -skip qtactiveqt -skip qtcharts -skip qtdoc -skip qtlocation -skip qtremoteobjects -skip qtserialbus -skip qtwebchannel -skip qtwebview -skip qtandroidextras -skip qtconnectivity -skip qtgamepad -skip qtmacextras -skip qtpurchasing -skip qtscript -skip qttranslations -skip qtwebengine -skip qtwinextras -skip qtdatavis3d -skip qtgraphicaleffects -skip qtmultimedia -skip qtquickcontrols -skip qtscxml -skip qtspeech -skip qtvirtualkeyboard -skip qtwebglplugin -skip qtx11extras -skip qt3d -skip qtcanvas3d -skip qtdeclarative -skip qtimageformats -skip qtnetworkauth -skip qtquickcontrols2 -skip qtsensors -skip qtwayland -skip qtwebsockets -skip qtxmlpattern
OK, now you have to settle in for a long build. In my case, it was just over an hour.
You know it.
If you’ve made it here, you now have a 64-bit MinGW development kit for Qt, installed side-by-side with your native vanilla GCC development kit. Now we need to configure Qt Creator to use it. If you’re a Qt developer, this part is probably trivial.
Add The Compiler
Add The Qt Version
Note that the warning is because there’s no qmlscene installed – this is not needed for merely making Windows-targeted builds for QA and production.
Add The Development Kit
Add a new Kit that references the new Compiler and new Qt Version. Note that the warning is because there’s no Debugger configured for this kit; we didn’t install the MinGW GDB as you would not use it to debug a Windows build on a Linux host. Instead you would debug using the native GCC and GDB.
So in reality, it’s not hard. The actual building of Qt is long, but the level of challenge is low.
The only real challenge lies in the relative dearth of clear information available on the web. Maybe I’m just bad at Googling, but it surprises me that this isn’t front and centre at qt.org.
The steps presented here were pieced together with the help of the following links, and a few hours of trial and error: