This tutorial was written for CodeCall
How can this work:
The fundamental basis of this is: MinGW, a minimalist GNU Compiler Collections (GCC) and GNU BinUtils port to the Windows platform. This means that the inner workings of the compiler toolchain on Linux contain the same inner workings as the port on the Windows platform and can cross compile with little intervention.
The MinGW cross-compile toolchain for Linux may be found here:
Download MinGW - Minimalist GNU for Windows from SourceForge.net
You can download this to a temporary directory of your choosing and extract it. This tutorial will assume you had saved the archive to /tmp/.
Extracting the MinGW32 build toolchain:
You will require basic tools to extract the .tar.bz2 archive which should be included in most distributions:
cd /tmp/ tar xjvf ./x86-mingw32-build-1.0.1-sh.tar.bz2Now the folder x86-mingw32-build-1.0.1 will be created. Browse to it:
cd x86-mingw32-build-1.0.1There will be a number of configuration files and two .sh shell scripts. You may first need to permit Linux to run the shell script, so you should run the following commands:
chmod 755 x86-mingw32-build.sh sh ./x86-mingw32-build.shThere will be a number of questions it will ask you. The first question will ask if you want to select individual components, type "NO" and then press enter as you will wish to install each of them automatically.
The next few questions you can safely use the defaults, but if it asks you if you want to install other compilers such as fortran (f77) and obj-c select 'no' as we will not be using that. Later on, one of the default paths it will ask you to install into will be /home/(username)/mingw32, this will be ideal for your setup so you can safely do this.
Note: After the questioning is completed it will start a long configure and install. Make SURE you are installing in a folder writable by you, and leave a good 5-10 minutes for it to configure!
Verifying it is installed!
Congrats, assuming the script reported there is nothing else to do, and is done then your build scripts should successfully be placed into your user folder.
You may access it like this:
cd $HOME/mingw32/bin/ lsYou will notice a pile of executable files, these are the MinGW32 version ports of GCC of which can be used in this platform.
If you see "i386-mingw32-gcc" listed in the folder you have got it installed!
Setting up the MinGW32 compiler:
The first thing you will wish to do is set up your PATH variable to include the bin directory. This will be dependent on your system, so I will list the most used shell configuration (BASH).
Adding it to your PATH:
Open up your bash profile (or create one if non-existant) with your favourite editor. Lets work with nano:
nano $HOME/.bash_profileCreate a new line and add the following:
PATH=$PATH:$HOME/mingw32/bin/Make sure you save it! Now open up any new console and type "i386-mingw32-gcc" without quotes and hit enter, you will notice it is run without typing the $HOME/ming32/bin/ folder each time, you are in business.
Compiling an EXE with MinGW32 (i386-mingw32-gcc):
All we will need to test the functionality of the build toolchain we just installed is a simple C or CPP file. Lets create a file named "win.c" in any folder and compile it.
Try it with the following contents:
win.c:
#include <stdio.h>
int main() {
printf("I am running on Windows!\n");
}Once you have saved the file:You will compile it similiarily to your Linux files with this command:
i386-mingw32-gcc -Wall ./win.c -o ./win.exeAnd there you have it:
you have officially compiled a working Windows executable in Linux.

-------------------------------------------------------------------------------------
For extras, you may install WINE on your distribution and test win.exe without leaving Linux, to prove it works even with a basic Windows interpreter!

Feel free to register and leave a comment or question if you have any problems. I will gladly help out and answer any questions you may have or accept any feedback.
Edited by Alexander, 07 November 2011 - 03:31 PM.
(Screenshots!)


Sign In
Create Account

Back to top










