Tutorial for cross-compiling?

Erik van Pienbroek erik at vanpienbroek.nl
Mon Jun 18 19:54:26 UTC 2012


Georgios Petasis schreef op ma 18-06-2012 om 21:34 [+0300]:
> Is there a tutorial available on what to install and how to use the 
> windows cross-compiler?
> I haven't managed to find anything recent.

Hi,

I must admit that our documentation is rather slim at the moment. One of
the goals I personally have for the coming months is to improve this
situation by writing more documentation and tutorials.

For now, here's some documentation to get you started:

Introduction
------------
As of Fedora 17 it is possible to easily build (cross-compile) binaries
for the win32 and win64 targets. This is realized using the mingw-w64
toolchain: http://mingw-w64.sf.net/. Using this toolchain allows you to
build binaries for the following programming languages: C, C++,
Objective-C, Objective-C++ and Fortran.

Infrastructure
--------------

To be able to build binaries for the win32 or win64 targets you need to
install some packages using yum or the graphical software installer.

Here's a chart indicating which packages provide which functionality:

                   +----------------------+----------------------+
                   |        win32         |        win64         |
+------------------+----------------------+----------------------+
| C compiler       | mingw32-gcc          | mingw64-gcc          |
+------------------+----------------------+----------------------+
| C++ compiler     | mingw32-gcc-c++      | mingw64-gcc-c++      |
+------------------+----------------------+----------------------+
| ObjC compiler    | mingw32-gcc-objc     | mingw64-gcc-objc     |
+------------------+----------------------+----------------------+
| ObjC++ compiler  | mingw32-gcc-objc++   | mingw64-gcc-objc++   |
+------------------+----------------------+----------------------+
| Fortran compiler | mingw32-gcc-gfortran | mingw64-gcc-gfortran |
+------------------+----------------------+----------------------+

Once you've installed one (or more) of these packages you should have a
new folder on your filesystem
called /usr/i686-w64-mingw32/sys-root/mingw (for win32)
or /usr/x86_64-w64-mingw32/sys-root/mingw (for win64). These folders
contain various pieces belonging to the mingw-w64 toolchain, for example
the headers belonging to the Windows API and the import libraries for
the default Windows API libraries (like shell32.dll and winsock2.dll). 

Compiling code
--------------

Now that you've installed a compiler it's time to use it. See the
following chart to find out what command you should use to run the
compiler:

+------------------+-----------------------------+
| C compiler       |                             |
|            win32 | i686-w64-mingw32-gcc        |
|            win64 | x86_64-w64-mingw32-gcc      |
+------------------+-----------------------------+
| C++ compiler     |                             |
|            win32 | i686-w64-mingw32-g++        |
|            win64 | x86_64-w64-mingw32-g++      |
+------------------+-----------------------------+
| ObjC compiler    |                             |
|            win32 | i686-w64-mingw32-gcc        |
|            win64 | x86_64-w64-mingw32-gcc      |
+------------------+-----------------------------+
| ObjC++ compiler  |                             |
|            win32 | i686-w64-mingw32-g++        |
|            win64 | x86_64-w64-mingw32-g++      |
+------------------+-----------------------------+
| Fortran compiler |                             |
|            win32 | i686-w64-mingw32-gfortran   |
|            win64 | x86_64-w64-mingw32-gfortran |
+------------------+-----------------------------+

So if you want to compile a .cpp file and make it an executable for the
win64 target, then you could using this command:
  x86_64-w64-mingw32-g++ main.cpp -o my_project.exe

For a full list of all command line arguments which are supported by the
various compilers, you can check out their man-pages, for example:
  man x86_64-w64-mingw32-g++

Building autoconf-based projects
--------------------------------

Most open source projects are using the autoconf infrastructure
(./configure; make; make install). This type of projects can be built
very easily for the win32 or win64 targets. Just install one of the
compiler packages mentioned earlier and replace ./configure with either
'mingw32-configure' or 'mingw64-configure' (depending whether you want
to build for the win32 or the win64 target). The mingw32-configure and
mingw64-configure scripts are helper scripts which automatically call
the ./configure script with the correct environment set

Once the mingw32-configure/mingw64-configure call has completed, you can
call 'make' and 'make install' just as you would usually do to build and
install a package

It is also possible to pass additional arguments to the
mingw32-configure/mingw64-configure calls, for example:
  mingw64-configure --enable-foo --disable-bar

When using the mingw32-configure/mingw64-configure scripts, the files
belonging to the package in question will automatically be installed to
either /usr/i686-w64-mingw32/sys-root/mingw
or /usr/x86-64-w64-mingw32/sys-root/mingw when you run 'make install'.
That way you can keep all cross-compiled libraries and executables in
one central location

Building CMake-based projects
-----------------------------

Another popular build system is CMake. Like with the
mingw32-configure/mingw64-configure scripts we've also made it easy to
build packaging using CMake. For this, you need to have the cmake
package installed first. Then you can use the mingw32-cmake or
mingw64-cmake commands to run CMake with the correct environment set for
the given target. Once the mingw32-cmake/mingw64-cmake command has
completed, just use 'make' and 'make install'

Building QMake-based projects
-----------------------------

The Qt project also has written a build system called qmake (actually:
qmake-qt4). This build system is used by various packages which are Qt
based. For this build system we've also prepared tools to make
cross-compilation for the win32 and win64 targets easy. Just install the
packages mingw32-qt-qmake or mingw64-qt-qmake. Once these are installed
you can execute the command i686-w64-mingw32-qmake-qt4 or
x86_64-w64-mingw32-qmake-qt4 followed by 'make' and 'make install'

Writing .spec files
-------------------

All the helper scripts which are mentioned earlier can also be used when
building RPMS using a .spec file. To find out more about writing .spec
files to build cross-compiled packages you should take a look at the
Fedora MinGW packaging guidelines:
https://fedoraproject.org/wiki/Packaging:MinGW
This set of guidelines also contains an example .spec file



Is this documentation enough to get you started? If you want to know
more about a certain subject, just yell and we'll try to write
documentation about it.

Kind regards,

Erik van Pienbroek




More information about the mingw mailing list