Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure Qt for cross-compilation from Linux to Windows target?

I want to cross compile the Qt libraries (and eventually my application) for a Windows x86_64 target using a Linux x86_64 host machine. I feel like I am close, but I may have a fundamental misunderstanding of some parts of this process.

I began by installing all the mingw packages on my Fedora machine and then modifying the win32-g++ qmake.conf file to fit my environment. However, I seem to be getting stuck with some seemingly obvious configure options for Qt: -platform and -xplatform. Qt documentation says that -platform should be the host machine architecture (where you are compiling) and -xplatform should be the target platform for which you wish to deploy. In my case, I set -platform linux-g++-64 and -xplatform linux-win32-g++ where linux-win32-g++ is my modified win32-g++ configuration.

My problem is that, after executing configure with these options, I see that it invokes my system's compiler instead of the cross compiler (x86_64-w64-mingw32-gcc). If I omit the -xplatform option and set -platform to my target spec (linux-win32-g++), it invokes the cross compiler but then errors when it finds some Unix related functions aren't defined.

Here is some output from my latest attempt: http://pastebin.com/QCpKSNev.

Questions:

  1. When cross-compiling something like Qt for Windows from a Linux host, should the native compiler ever be invoked? That is, during a cross compilation process, shouldn't we use only the cross compiler? I don't see why Qt's configure script tries to invoke my system's native compiler when I specify the -xplatform option.

  2. If I'm using a mingw cross-compiler, when will I have to deal with a specs file? Spec files for GCC are still sort of a mystery to me, so I am wondering if some background here will help me.

  3. In general, beyond specifying a cross compiler in my qmake.conf, what else might I need to consider?

like image 552
Mr. Shickadance Avatar asked Jun 07 '12 15:06

Mr. Shickadance


People also ask

What is cross platform compilation?

A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a compiler that runs on a PC but generates code that runs on an Android smartphone is a cross compiler.


1 Answers

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.

like image 163
tshepang Avatar answered Sep 20 '22 06:09

tshepang