This tutorial will run through using gfortran on DSC machines.

Click on any image to view it in a lightbox.

The Basics

For starters, let's get MinGW/gfortran 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 gfortran compiler on your program to generate an executable.

gfortran HelloWorld.f90

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. gfortran 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 two files: one that contains a factorial algorithm inside a module, and one that makes use of that module. The two 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 module (Whee.f90). Then, compile the Whee module without linking (the -c option). This will produce two object files, Whee.mod and Whee.o, that are ready to be linked into some other program.

gfortran -c Whee.f90

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

gfortran -c -I"../SomeOfMyLibraryCode" FacFive.f90

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.

gfortran -o Fac.exe ../SomeOfMyLibraryCode/Whee.o FacFive.o