Thursday, May 25, 2006

MinGW What?

From the MinGW Website:

A collection of freely available and freely distributable Windows specific header files and import libraries combined with GNU toolsets that allow one to produce native Windows programs that do not rely on any 3rd-party C runtime DLLs.

Why do I need it?

MinGW offers you native Windows versions of the GNU Compiler Collection (GCC) and several other developer tools that help you create applications using C, C++, Objective-C, Ada, Fortran, etc. for free. You no longer need to worry about purchasing an expensive compiler suite for your development purposes. GCC is known to work on several platforms and architectures, and is one of the most standards-compliant (not entirely in some areas) compiler collections available.


How do I install it?

Apparently, this is the one question that I find a lot of people asking. The reason is that the MinGW downloads section appears overly complicated to the absolute beginner. Fear not, however, my dear friend--installing and getting MinGW to work is not as difficult as it might appear to be.

There are two approaches to installing MinGW:
  1. Web installer guided installation
  2. Manual installation
1. Web Installation

Using the web installer is the easiest way to install MinGW on your Windows system. All you need to do is download the installer program from www.mingw.org, run the program, and follow the instructions presented. I recommend using this method to people that are new to MinGW. However, note that the web installer does not install everything that MinGW has to offer.

The current installer program is available here:
http://prdownloads.sf.net/mingw/MinGW-5.0.2.exe?download

(Please see the "Telling Windows about MinGW" section below for post-install configuration instructions.)

2. Manual Installation

Manual installation is not for the faint of heart, right? Wrong! It's extremely easy to do, but it is easy to screw up too. Do not be afraid because you can always start afresh.

Before I tell you what you need to download, let's look at the several tools that MinGW has to offer to you.

  1. Compilers/Interpreters for several languages including C, C++, Objective-C, Java, Ada, and Fortran.
  2. Debuggers and crash reporting tools.
  3. Assembler/Disassembler.
  4. Tools to play with binary executables and object files (GNU Binutils).
  5. A cut-down UNIX shell like environment called MSYS.
  6. Some GNU utilities.
  7. Windows API header files.
  8. Build tools including GNU make, autoconf, automake, and libtool.
  9. Some MinGW-specific tools.
If you do not know what these tools are, no worries. We just need to get some of them installed properly and we'll be good to go.

MinGW makes three types of release:

  1. Candidate - The future
  2. Current - The present
  3. Previous - The past

I generally settle for the candidate release, but for a production environment you might need to choose a rather stable and well-tested release (ask your project coordinator/systems administrator for help with this). Within each release there are two types of compressed archives: source (src) tarballs and binary (bin) tarballs. You most certainly need the binary tarballs.

Tar files (.tar) are commonly referred to as tarballs. These can be compressed by tools like gzip or bzip2 to create compressed tarballs (.tar.gz or .tar.bz2).
Downloading MinGW

The files that you will need to get are enlisted below. (Please note that the version numbers displayed here may not match those on the MinGW download page--use your judgment and proceed.)

gcc-core-3.4.5-20060117-1.tar.gz - (required) The core gcc compiler.
gcc-g++-3.4.5-20060117-1.tar.gz - C++ compiler and libraries
gcc-objc-3.4.5-20060117-1.tar.gz - Objective-C compiler and runtime
gcc-java-3.4.5-20060117-1.tar.gz - Java compiler and associated libraries
gcc-g77-3.4.5-20060117-1.tar.gz - Fortran 77 compiler
gcc-ada-3.4.5-20060117-1.tar.gz - Ada compiler
binutils-2.16.91-20060119-1.tar.gz - Linker, Assembler, Disassembler, tools to play with object and executable files, etc.
mingw-utils-0.3.tar.gz - General GNU tools built specifically for use with MinGW.
w32api-3.6.tar.gz - Windows API header files.
gdb-5.2.1-1.exe - GNU Debugger (GDB)
mingw32-make-3.80.0-3.exe - GNU Make

What next?

  1. Once you have obtained all these files, place all of them into a single directory.
  2. Create a directory called "mingw" in this directory.
  3. Extract the contents of the files ending with .tar.gz in their names into the mingw directory. (If you are asked to overwrite files, please choose to.)
  4. Copy this directory over to your system root drive partition (C: for most folks) so the path where MinGW is located now becomes X:\mingw, for example, where X is your system root drive letter.
  5. Run the GDB and MinGW32-Make installers and install them in X:\mingw.
That's it? Not yet, smartypants. We have one step remaining. The most important one that is common to both the web installer and the manual installation. That is the subject of the next section.

Telling Windows About MinGW
Have you ever wondered how when you type and execute a particular command in the Command Window, Windows figures out exactly where the executable for that command is located and runs it? Well, you don't need to be a rocket scientist to figure out that this information must have been told to Windows by someone and that it is stored somewhere.

In our case, here, you're the one that tells Windows where MinGW is installed and to do just that, you need to edit the system registry (*shudder*). Not really. I was just kidding. All you need to do is edit the environment variable, Path, and append X:\mingw\bin to it--remember that ';' is used as a path separator on Windows.

Very Important Note: I won't be teaching you how to set up an environment variable. You can ask Google, Luke! Or, perhaps, I can write another blog entry describing how to work with environment variables in the near future.

How do I use it?

Good question. We are going to use the GCC C compiler in our simple example that follows. Create a text file and add the following code to it. Save the file with the name 'first.c' sans quotes.


#include <stdio.h>

int
main (void)
{
puts ("Ah! My program compiles and runs\n");
return 0;
}



Now open a command window and 'cd' to the directory where you saved your source file. Then type out this command.


C:\home\code\c> gcc -ansi -pedantic -W -Wall -O2 -o first first.c

C:\home\code\c> first.exe
Ah! My program compiles and runs


C:\home\code\c>


Your program works and so does MinGW. *drumroll* That's it for now, though. Adios.