Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cross-compile with MinGW on Linux for Windows?

I'm trying to compile a Qt5 application for Windows on Linux with MinGW.

I'm using Travis-CI to get continuous builds of two windows executables (win32, win64).

I've set up a build matrix, which contains the definition of the different MinGW dependencies. The before-install section defines the Qt, MinGW and gcc dependencies. The packages are fetched during install.

This is my .travis.yml:

language: cpp   
  matrix:
fast_finish: true
include:
  # cross-compile using mingw
  - compiler: ": w64"
    env: PLATFORM="mingw32" ARCH="x86_64" BITSIZE=64 HOST="x86_64"
  - compiler: ": w32"
    env: PLATFORM="mingw32" ARCH="x86" BITSIZE=32 HOST="i686"

install:
  - sudo add-apt-repository --yes ppa:beineri/opt-qt54                   # < Qt
  - sudo add-apt-repository --yes ppa:tobydox/mingw-x-precise            # < MinGW
  - sudo apt-get update -qq
  - uname -m
  - sudo apt-get install qt54base qt54imageformats qt54tools -y -qq
  - export QTDIR=/opt/qt54
  - export PATH=$QTDIR/bin:$PATH
  - export LD_LIBRARY_PATH=$QTDIR/lib/:$QTDIR/lib/`uname -m`-linux-gnu:$LD_LIBRARY_PATH
  - sudo apt-get install -y cloog-isl mingw32
  - if [ $BITSIZE == 32 ]; then sudo apt-get install -y mingw32-x-binutils mingw32-x-gcc mingw32-x-runtime; fi
  - if [ $BITSIZE == 64 ]; then sudo apt-get install -y mingw64-x-binutils mingw64-x-gcc mingw64-x-runtime; fi
  - export MINGW=/opt/mingw$BITSIZE
  - export PATH=$MINGW/bin:$PATH
  - export CC=$HOST-w64-mingw32-gcc
  - export CXX=$HOST-w64-mingw32-g++

script:
  - qmake -v
  - qmake wpnxm-servercontrolpanel.pro CONFIG+=release QMAKE_CC=$CC QMAKE_CXX=$CXX
  - gcc -v
  - gcc -dumpmachine
  - export CMAKE_OPTS="-DUSE_WERROR=ON"
  - make -j2 VERBOSE=1

The build fails.

The output of make contains

 i686-w64-mingw32-g++ -c -pipe -O2 -std=c++0x -D_REENTRANT -Wall -W -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I/opt/qt54/mkspecs/linux-g++ -I. -I/opt/qt54/include -I/opt/qt54/include/QtWidgets -I/opt/qt54/include/QtGui -I/opt/qt54/include/QtNetwork -I/opt/qt54/include/QtCore -I. -I. -o mainwindow.o src/mainwindow.cpp
src/mainwindow.cpp:1:0: warning: -fPIC ignored for target (all code is position independent)
 /*
 ^
src/mainwindow.cpp:41:21: fatal error: QCheckbox: No such file or directory
 #include <QCheckbox>
                     ^
compilation terminated.

Found out: it's QCheckBox.

I'm not sure, if i fetch the right version of Qt5.

Which changes are needed to build the executables?

like image 239
Jens A. Koch Avatar asked Jan 26 '15 17:01

Jens A. Koch


People also ask

Can Linux MinGW compile?

MinGW (historically, MinGW32) is a toolchain used to cross-compile Windows binaries on Linux or any other OS. It can also be used natively on Windows to avoid using Visual Studio, etc.

Can you compile Windows EXE on Linux?

All you have to do is install either VirtualBox or VMWare, create a new virtual machine, and set up Windows on it. Then, you can simply start the virtual machine and run Windows inside your Linux-based OS. This way, you can run EXE files and other programs like you would normally do on a Windows-only computer.

Can GCC cross-compile to Windows?

You can cross-compile and -link Windows applications with it. There's a tutorial here at the Code::Blocks forum. Mind that the command changes to x86_64-w64-mingw32-gcc-win32 , for example.


1 Answers

First of all, make sure you have the mingw32, mingw32-binutils and mingw32-runtime packages installed. The runtime package contains all the common Windows headers you'll need for cross-compiling, like the missing "windows.h".

Also be aware that you're trying to cross-compile using a Linux version of the Qt library. This will fail during the linking stage. You need mingw32 versions (.DLLs) of all the dependencies you require.

Since Travis don't offer mingw32 Qt packages yet, you will have to install them from a third party source (see http://docs.travis-ci.com/user/installing-dependencies/) like https://launchpad.net/~tobydox/+ppa-packages

like image 170
muesli Avatar answered Oct 06 '22 04:10

muesli