via Docker
Statically cross-compiling Qt to Windows from Linux is a headache. You'll most likely spend a lot of time fighting Qt, CMake, Windows, etc.
Thankfully, we have solved that problem and share our Dockerfile that uses mingw to cross-compile in only 2 commands.
Resulting in:
.exe
- 60MB.The reason this article covers a QtQuick application is that these are harder to statically cross-compile than QtWidgets. So, this Dockerfile has both types covered (QtQuick, QtWidgets) which is nice.
We will be cloning qt5-qml-cmake-hello-world, a starter template.
git clone https://github.com/kroketio/qt5-qml-cmake-hello-world.git
Next up, we'll prepare our docker container.
The Dockerfile, based on Ubuntu 20.04, compiles the following statically:
This takes around ~15 minutes depending on CPU speed and threads.
docker build -f Dockerfile.windows --tag app:win --build-arg THREADS=8 .
Note: You only need to build the base image once.
Also, you may wonder what Monero's depends build configuration (a derivative of Bitcoin's depends) is doing inside the Dockerfile: this toolchain supports reproducible builds. A topic for another article perhaps.
The resulting binary will be over at: build/x86_64-w64-mingw32/release/qml_hello.exe
docker run --rm -it -v $PWD:/app -w /app app:win \
sh -c 'make windows root=/depends target=x86_64-w64-mingw32 -j8'
That's it, your application will run on Windows 7, 8, 10, [...].
CMakeLists.txt
and src/CMakeLists.txt
have CMake code
to facilitate static compiles. You would have to copy that code. Just
search for the if(MINGW)
, if(WIN32)
, and if(STATIC)
sections, and copy
as you see fit.docker run
command uses the Makefile
to eventually call cmake
. This is
because the CMake command is long. If you want to pass CMake more flags, define
them there.For the past few years we have used this Dockerfile to create static Windows Qt builds in a CI/CD environment. We think it is a good replacement for both MSYS2 compiles on Windows or MXE-based toolchains.