Building Windows executable (binaries) from Linux environments using Qt Creator
Working with Qt gives great advantage of developing cross-platform programs. But the term cross-platform means that you need to compile each program for its target OS independently. Term cross-platform differs from multi-platform in this aspect. In multi-platform compiling like when you compile Java programs you build jar files which makes you capable of running the same jar file inside any platform hosting Java Run-time Libraries. It’s like running an application within a virtual machine and provides you with a little overhead and run time penalty. But when you compile a Qt application for a specific platform you get the native binaries with native assembly code for that specific OS. Of course, the drawback of this aspect is that you have to it independently for each platforms and only your source code remains the same across different platforms.
Compiling for Windows from Linux
So, if your like me interested in building your desktop software for Windows and Linux at the same time. This will save you some time. And you don’t need to switch back and forth between your operating systems after making any changes to your program.
All you need is package called MXE from mxe.cc . The setup is straight forward and the site itself contains tutorials with different methods like using pre-build binaries that comes with unofficial Debian repositories or building for own machine from the git source. I am taking the second approach and making every thing just in some simple steps.
1-install requirement packages:
you can find list of required packages from this source: http://mxe.cc/#requirements
for Debian x64 including Ubuntu and Mint you could use the following command in your terminal
The last two packages are for x64 you don’t need them in x86 systems.
2-Obtain the package from git
Clone the package from github and then enter the cloned folder from git repository
git clone https://github.com/mxe/mxe.git
If you already have the package cd to package location and only use
3-Build the package
All you need is to run the make command. MXE Makefile will download the required files needed for building the package. The MXE_TARGET switch in the make command will help you declare your target platform. For example “x86_64-w64-mingw32.static” is used for static builds of Windows x64. Following you could find 2 examples. In the first one you could find almost all the variations you may need in the feature binaries in shared mode or static, targeting both x86 or x64 systems. But I recommend if you don’t have enough time choose one, because it takes fairly long time to build each. In the second one I added posix configuration is which is most helpfull if you are trying to write mulit-threaded applications using standard libraries thread and ofcousre I added support for freeglut (GUI toolkit for OpenGL) as well.
i686-w64-mingw32.static => static x86
x86_64-w64-mingw32.static => static x64
i686-w64-mingw32.shared => shared x86
x86_64-w64-mingw32.shared => shared x64
i686-w64-mingw32.static.posix => static x86 (POSIX configuration)
mxe$ make MXE_TARGETS=’x86_64-w64-mingw32.static i686-w64-mingw32.static i686-w64-mingw32.shared x86_64-w64-mingw32.shared’ qt5
mxe$ make MXE_TARGETS=i686-w64-mingw32.static.posix qt5 freeglut
Notice that if you are active directory is different from the folder where the MXE Makefile exist you can define a different Makefile location with switch -C, this is most useful when you want to batch the whole commands in a single file for your later convenience.
make -C “./mxe” MXE_TARGETS=’x86_64-w64-mingw32.static i686-w64-mingw32.static i686-w64-mingw32.shared x86_64-w64-mingw32.shared’ qt5
You may chose one or all of the above whatever is your taste.
Frankly, I tried using -j4 to parallel the make process to reduce the time, but it didn’t effect too much.
4-Configuring Qt Creator to use the MXE
After the build is complete you could find all the files you need in the folder ‘mxe/usr/bin’
However, this point is not required in our tutorial, but it is worth mentioning. The following files are those you may need mostly for building non-Qt C/C++ programs. You may use them like ordinary gcc or g++ command which you use for building C/C++ binaries for Linux. But remember you need to export the ‘mxe/usr/bin’ path or add it to system wide path environment before using those commands. Otherwise your build may fail.
And the following are those we will need for next section to configure our Qt Creator for building our Qt source code.
5-Configuring Qt Creator to use the MXE
Finally, create a sample test application from qt and the following screenshots with their captions will guide setting up the Qt Creator for MXE in the next 10 additional steps: