This tutorial will run through using g++ on DSC machines. If you're writing a pure C application, all of the following also applies to gcc.

Click on any image to view it in a lightbox.

The Basics

For starters, let's get MinGW/g++ to compile and run the eponymous hello world program. The first thing to do is navigate to your preferred source code storage location, and write the hello world program.

The Hello World program in a usable folder.

Then, start MinGW.

Use the search feature in the Start menu if you're stymied.

And then navigate the MinGW shell so that its working directory is your program location.

Use cd to change directory, ls to list contents of the current directory.

At this point, you can run the g++ compiler on your program to generate an executable.

g++ HelloWorld.cpp

Then, run the executable. You may run it from inside MinGW. However, since it's a native Windows exectuable, you may also run it from the command prompt, from a batch script, or by double clicking on the executable in the explorer window. For this program, running it from the explorer window merely flashes a console window.

Run a.exe in a prompt to run your compiled program.

Other Features

At some point you may build up a code library that you would like to reference. g++ contains options to ease the reuse of old code. In order to explore these options, lets compute the factorial of five. First, go to your prefered source directory, and write the program. The program is split up into three files: one that contains a factorial algorithm, coupled with a header file, and one that makes use of that module. The files are split into disparate directories.

A factorial function in a module, located in a different folder than the main program.

Start up MinGW, and navigate it to the directory containing the factorial function (Whee.*). Then, compile the Whee.cpp file without linking (the -c option). This will produce one object file, Whee.o, that is ready to be linked into some other program.

g++ -c Whee.cpp

Then, navigate to the directory containing the main program (FacFive.cpp). Then compile it without linking. In order to compile, g++ will need to know where any external code libraries are. The -I option can be used for this.

g++ -c -I"../SomeOfMyLibraryCode" FacFive.cpp

Finally, link all the object files together, specifying the name of the executable with the -o option. While this example does not use any dll libraries, if your own code uses them, you'll need to use the -L option. Run the program, and you should find that 5! = 120.

g++ -o Fac.exe ../SomeOfMyLibraryCode/Whee.o FacFive.o