Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Making .exe files with Linux (cross compiling)

extract compile

  • Please log in to reply
2 replies to this topic

#1 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 15 November 2010 - 04:29 AM

CodeCall Intro: My tutorial today will be about how to compile an executable (.exe) for Windows on a Linux system. This will be a method to cross-compile to Windows without requiring a virtual machine or emulation on your end and can speed testing when required.

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.bz2
Now the folder x86-mingw32-build-1.0.1 will be created. Browse to it:
cd x86-mingw32-build-1.0.1
There 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.sh
There 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/
ls
You 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_profile
Create 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.exe
And there you have it:
you have officially compiled a working Windows executable in Linux.

Posted Image

-------------------------------------------------------------------------------------


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!


Posted Image


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!)

  • 2

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#2 DEViANT

DEViANT

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 265 posts

Posted 16 November 2010 - 01:54 AM

Thats pretty sweet. Gives programmers less and less reason to keep clinging on to Microsoft Windows!

Good tutorial. Thanks :)
  • 0

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#3 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Programming Language:C

Posted 16 November 2010 - 10:25 PM

I always ran MinGW under Wine, but this works too. Very nice :)

+rep (I know your rep is disabled, but it can still be symbolic or something :P)
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)





Also tagged with one or more of these keywords: extract, compile

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download