Building a windows executable in qt on a Linux system
I have been having the hardest time trying to compile a .pro file in Linux to make a windows executable. I have read all the links in the world on using mingw and nothing has worked as of yet. Is there something I'm missing or does anyone have a really good step by step on how to do this?
Ubuntu 14.04.2 QT 4.8
For QT4
The following steps are taken from an answer on a post on stack overflow, for building with qt4 if it works make sure to give them an upvote.
Just use M cross environment (MXE). It takes the pain out of the whole process:
-
Get it:
git clone https://github.com/mxe/mxe.git
Install build dependencies
-
Build Qt for Windows, its dependencies, and the cross-build tools; this will take about an hour on a fast machine with decent Internet access; the download is about 500MB:
cd mxe && make qt
-
Go to the directory of your app and add the cross-build tools to the PATH environment variable:
export PATH=<mxe root>/usr/bin:$PATH
Run the Qt Makefile generator tool then build:
<mxe root>/usr/i686-pc-mingw32/qt/bin/qmake && make
-
You should find the binary in the ./release directory:
wine release/foo.exe
Some notes:
Use the master branch of the MXE repository; it appears to get a lot more love from the development team.
The output is a 32-bit static binary, which will work well on 64-bit Windows.
For QT5
The following steps are taken from an answer on a post on stack overflow, for building with qt5 if it works make sure to give them an upvote.
Here are the full instructions:
-
Get it:
git clone https://github.com/mxe/mxe.git
Install build dependencies
-
Build Qt 5 for Windows:
cd mxe && make qtbase
This will first build its dependencies and the cross-build tools; It should take less than an hour on a fast machine with decent Internet access.
Due to the new modular nature of Qt 5, various major Qt components are now in different tarballs. The one selected above, qtbase, should give you enough functionality to run ordinary GUI apps, which is all I needed for my own (smallish) app.
If you want to build all of Qt 5 instead, you'll need to run
make qt5
(instead of make qtbase). Note that it will take a lot longer to complete, so be sure that you need the extra functionality. -
Get to the directory of your app, and run the Qt Makefile generator tool:
<mxe root>/usr/bin/i686-w64-mingw32.static-qmake-qt5
-
Build your project:
make
-
You should find the binary in the ./release directory:
wine release/foo.exe
Some notes:
This was tested on my 64-bit Debian 8, and on Windows of course.
The output is a 32-bit static executable, which will work well on 64-bit Windows.
If you want a 64-bit executable, build Qt with:
make MXE_TARGETS=x86_64-w64-mingw32.static qtbase
The default MXE_TARGETS value is i686-w64-mingw32.static.