Journal Articles

CVu Journal Vol 15, #3 - Jun 2003 + Programming Topics
Browse in : All > Journals > CVu > 153 (14)
All > Topics > Programming (877)
Any of these categories - All of these categories

Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.

Title: Quick Guide to MinGW (GCC for Windows)

Author: Administrator

Date: 03 August 2003 13:15:58 +01:00 or Sun, 03 August 2003 13:15:58 +01:00

Summary: 

Body: 

What is MinGW?

According to the MinGW website [website], MinGW is 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 DLLs. Unlike other ports of GCC to Windows, the runtime libraries are not distributed using Gnu's General Public License (GPL). The full license for MinGW can be found on the License page [license] of the website.

So, in simple terms MinGW is GCC [gcc] for Windows complete with a Windows API.

Getting and Installing MinGW

Full information about the various components of MinGW and links to the packages are available from the MinGW download page [download]. There are a number of different components, but only one is needed to install a basic MinGW and get started. The current version of MinGW (at the time of writing) is Version 2.0.0, although it is actually version 3.2 of GCC. The Windows installation package is called MinGW-2.0.0-3.exe. Download this file, run it and follow the instructions to install MinGW.

To use MinGW from the command line, the path to the MinGW BIN folder must be added to Windows Path environment variable. To do this on Windows 2000 go to the Control Panel and double click the System Icon. Then click the Advanced tab and click the Environment Variables button. Select 'path' in the User Variables for ...' list box and click Edit. This brings up the 'Edit User Variable' dialog box.

Figure 1. The Edit User Variable dialog box

The Edit User Variable dialog box

Move the cursor to the end of the 'Variable Value' edit box, type a semicolon and add the path the the MinGW bin directory. The default path is C:\MinGW\bin. Then click Ok. To test MinGW open a command prompt, type the following and press return:

g++ -version

This should give something similar to the following output:

g++ (GCC) 3.2 (mingw special 20020817-1)
Copyright (C) 2002 Free Software Foundation,
Inc.
This is free software; see the source for
copying conditions. There is NO
warranty; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.

Hello World with MinGW

The classic Hello World program is very easy with MinGW. Simply create a file called something like HelloWorld.cpp and write the Standard C++ Hello World program into it:

// HelloWorld.cpp
#include <iostream>
#include <cstdlib>
int main() {
  std::cout << "Hello, World!"
            << std::endl;
  return EXIT_SUCCESS;
}

To compile the Hello World program, type the following at the command line from the directory which contains HelloWorld.cpp:

g++ -o HelloWorld HelloWorld.cpp

This should create an executable called HelloWorld.exe. Running the executable should display the "Hello, World!" message.

Simple Make Files with MinGW

MinGW has a modified version of GNU Make [make] called mingw32-make.exe. The MinGW port of Make operates more as Make was intended to operate and gives less headaches during execution than the 'native' MSVCRT dependent version. For more detailed information on using Make see the GNU Make Manual [make-manual].

To create a simple make file for the Hello World example, create a file called makefile, and enter the following rules:

# HelloWorld Makefile
HelloWorld : HelloWorld.o
g++ -o HelloWorld.exe HelloWorld.o
HelloWorld.o : HelloWorld.cpp
g++ -c HelloWorld.cpp
clean:
del *.exe *.o

To build HelloWorld.exe executable enter the following at the command line and press return:

mingw32-make

To clean the project, or in other words remove the object files and executables, type the following at the command line and press return:

mingw32-make clean

Of course a make file for a single source file program such as Hello World is overkill. The above example is intended to be a starting point for larger projects.

The source code and make file [source] used in this article is available to download from my website.

References

[website] MinGW Website: http://www.mingw.org

[license] MinGW License: http://www.mingw.org/licensing.shtml

[gcc] GNU GCC: http://gcc.gnu.org/

[download] MinGW Download Page: http://www.mingw.org/download.shtml

Notes: 

More fields may be available via dynamicdata ..