Using MinGW cross-compiler mini-HOWTO

 ... or how to use a GCC that runs in Linux and generates win32 binary files



Author: Alex Shaduri <ashaduri at yandex.ru>


Sites you should visit for additional info:
http://members.lycos.co.uk/alexv6/ - My site, this howto can be found there, as well as
	pre-compiled mingw cross compiler and some useful scripts.
http://www.mingw.org - The project itself. You can download gcc/binutils patches and
	sources here, as well as the native compiler.
http://www.libsdl.org/extras/win32/cross/ - Sam Lantinga's (aka Slouken) page
	with gcc-2.95 mingw as well as cross-scripts. My version of cross-scripts
	is based on his work.


Note: This HOWTO assumes that you have downloaded the cross-compiler
and cross-scripts from my site (link above). If you haven't done this already,
I recommend that you download this software, unless you are qualified
in this subject. The cross-compiler should be unpacked to /opt/cross-tools,
and the cross-scripts should be put into some $PATH-ed directory,
such as /usr/local/bin. (Note: The cross-scripts is not a requirement, but
it proves to be very useful in everyday work).


We will use /opt/cross-tools as installation prefix (Why? Because of my
partition layout, also because self-contained things that require a whole
directory should be installed in /opt, not in /usr/local).


Advice: You should make /target a link like this:
# ln -s /opt/cross-tools/i386-mingw32msvc /target
This will ease installing some win32 libraries you'll find in the internet
and will save you from editing a bunch of files.


From now, when building software with a cross-compiler, use the cross-configure
and cross-make scripts, e.g.:
cd 
cross-configure --enable-something
cross-make
cross-make install
cd -

Note that all the options you pass to cross-configure and cross-make will be
automatically passed to the real ./configure and make.

You should read the cross-configure and cross-make scripts to
understand what they're doing. They are simple shell scripts,
so there's nothing difficult there.

Note: -pthread GCC option must be substituded with -mthreads option.
Keep in mind that -mthreads causes a mingw10.dll dependancy.

Of course, when compiling a package, you should have all the libraries
it needs installed in /target/lib, it can't use the libraries of your native system.

Almost any library compiled for MinGW win32 native version will
work without changes. Simply extract it to /opt/cross-tools/i386-mingw32msvc.
(Put linux binaries into /opt/cross-tools/bin, win32 binaries into
/target/bin (although they are of no use anyway), and win32 libraries
into /target/lib).

There's a strange library naming convention in win32. Suppose that
you have some library named "foo". The files for this library in /target/lib
should be:
libfoo-version.dll  - a real dll, needed when launching a program.
libfoo-version.dll.a  - import library. Needed for compiling.
libfoo-version.a  - static library, needed for static linking with this library.
libfoo-version.dll-def  - (sometimes present) this file lists the dll's exported symbols.


See http://gnuwin32.sourceforge.net/ for some precompiled win32
MinGW libraries.
The following site is a source for GTK w32 libraries:
http://www.gimp.org/~tml/gimp/win32/downloads.html  (gtk, png, tiff...)
Also, http://www.libsdl.org/ provides win32 versions of SDL libraries
for download.

You need dev packages (with .a and .h files) for compiling, .dll files for
running. You should put those .a files into /opt/cross-tools/i386-mingw32msvc/lib
and .h files into /opt/cross-tools/i386-mingw32msvc/include.
When launching the final program in windows, the .dll files should
be put somewhere where win32's linker will find them (like $PATH).


Additional useful software:
MSys (from mingw.org) is a cygwin-like set of tools that fits into 3MB and
contains some usefuls unix commands (make, rxvt, bash, vim...).


That's all the information I managed to write here. If you have some questions
please write them to me and I'll answer as soon as I can.
If you don't have any previous experience with GCC, I suggest that
you should learn it first. Also, some autoconf/automake experience won't
hurt.

Any comments, suggestions, flames, etc... are welcome.

Last update: Wed Jun 16 20:05:53 2004
(c) Alex Shaduri <ashaduri 'at' yandex.ru>