10

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

1
  • In addition to the answer that was shared, I will also share this interesting comment that could be useful for people finding this question (which is not what was asked in the question but apparently is an easier solution): "As a long time linux developer, I've found it's 1000x easier to build for each OS inside of a development environment for that OS." forum.qt.io/topic/94742/…
    – baptx
    Nov 10, 2021 at 17:30

1 Answer 1

10

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:

  1. Get it:

    git clone https://github.com/mxe/mxe.git
    
  2. Install build dependencies

  3. 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
    
  4. 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
    
  5. 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:

  1. Get it:

    git clone https://github.com/mxe/mxe.git
    
  2. Install build dependencies

  3. 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.

  4. Get to the directory of your app, and run the Qt Makefile generator tool:

    <mxe root>/usr/bin/i686-w64-mingw32.static-qmake-qt5
    
  5. Build your project:

    make
    
  6. 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.

5
  • Wine will slow the overall application,is there any other way ?
    – Arun Pal
    Feb 6, 2019 at 11:51
  • 2
    @arun-pal copy the exe to a windows machine and run it there if wine is inadequate.
    – Justin C
    Feb 20, 2019 at 19:44
  • You need to do qmake-qt5 before make Mar 26, 2020 at 17:15
  • 1
    You may need to add the cross-build tools to PATH when using QT5 as well. This instruction is only in the QT4 instructions here.
    – nivlac
    Oct 8, 2020 at 0:13
  • Instead of building Qt ourself, isn't it possible to cross-compile on Linux by downloading binaries / Windows SDK from the Qt website? And cross-compile directly in Qt Creator on Linux instead of using a third-party tool like MXE?
    – baptx
    Nov 10, 2021 at 19:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .