Cross-compile Qt for Windows on Linux platform

Vlad Lebowski
4 min readApr 8, 2020

--

Qt, Windows and Linux logo together

Some people don’t like Windows platform. Instead of creating dual boot system and compiling stuff on its native system to make life easier, developers are looking for articles like this.

I’ll show you how to compile Qt 5.13.2 for Windows using Linux environment, in our case it will be Debian 10.

Step 1 — How to compile?

Download source code of Qt using Qt Online Installer.

qt-unifiled-linux-x64-online.run inside Downloads folder

You probably won’t have permissions to execute, thus

sudo chmod a+x qt-unifiled-linux-x64-online.run; ./qt-unifiled-linux-x64-online.run

You can choose installation folder whatever you want. For example /opt/Qt.

Step: Choose intalling folder Qt. /home/aseg/Qt

Choose Sources of Qt which you want to compile.

Step: Select components. Check mark on point 5.13.2 -> Sources
Step: Installing Qt. Process of downloading and extract archives after downloading. Automated step no human interaction.

While its downloading and extracting archives, lets configure system. We will use x86_64_w64-mingw32.

  • x86_64-w64-mingw32: 64-bit Windows
  • i686-w64-mingw32: 32-bit Windows
sudo apt install --yes g++-mingw-w64

By default mingw on debian works in win32 mode which doesn’t support thread. If you want to know more I’ll give you key-words:

  • posix: enable C++11/C11 multithreading features. Makes libgcc depend on libwinpthreads.
  • win32: No C++11 multithreading features.

To fix this choose posix option after execute the commands above

sudo update-alternatives --config x86_64-w64-mingw32-g++
sudo update-alternatives --config x86_64-w64-mingw32-gcc

Before the compilation install the following packages:

sudo apt update; sudo apt --yes --install build-dep libxcb-xinerama0-dev build-essential perl python '^libxcb.*-dev' libx11-xcb-dev libglu1-mesa-dev libxrender-dev libxi-dev libxkbcommon-dev libxkbcommon-x11-dev flex bison gperf libicu-dev libxslt-dev ruby libssl-dev libxcursor-dev libxcomposite-dev libxdamage-dev libxrandr-dev libdbus-1-dev libfontconfig1-dev libcap-dev libxtst-dev libpulse-dev libudev-dev libpci-dev libnss3-dev libasound2-dev libxss-dev libegl1-mesa-dev gperf bison libasound2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libclang-6.0-dev llvm-6.0

For more information check https://wiki.qt.io/Building_Qt_5_from_Git.

When step Installing Qt is done, go to the folder where you downloaded sources. In my case:

cd ~/Qt/5.13.2/Src

We’re interested in a file named ./configure. To see what options do you have write

./configure --help

Let’s create folder where we will install our compiled Qt.

mkdir ~/Qt/5.13.2/x86_64-w64-mingw32

To configure I will use the following command

./configure -opensource -confirm-license -xplatform win32-g++ -device-option CROSS_COMPILE=/usr/bin/x86_64-w64-mingw32 -prefix /usr/Qt/5.13.2/x86_64-w64-mingw32 -nomake examples

You will see output where will be more details how your Qt was configured. If everything is okay just write

make -j$(nproc)
  • nproc prints the number of processing units available to the current process

Sometimes when you use more than 2 jobs it may fail. Even repeating the previous command won’t help and you need to use make clean and then repeat command. In this case it can take more time than if you choose 8 jobs instead of 2 jobs but you can take a chance :)

And now you can go and take a cup of coffee after that take another one and another one…

make install

Step 2 — How to use?

Lets create a file x86_64-w64-mingw32-toolcahin.cmake. To compile your project for Windows 10:

set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_VERSION 10)

set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)

set(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32/ /usr/local/x86_64-w64-mingw32/)
set(CMAKE_PREFIX_PATH ~/Qt/5.13.2/x86_64-w64-mingw32/)
...

And finally use your file with the following command

-DCMAKE_TOOLCHAIN_FILE=cmake/x86_64-w64-mingw32-toolcahin.cmake

Feel free to comment and correct me. Even if you have problems with following instruction you can write to me.

--

--