Page 1 of 1

Setting up MinGW for Fortran

Posted: Thu Aug 01, 2013 11:08 am
by jussij
The steps below describe how to setup the MinGW FORTRAN compiler.

1) Download the MinGW installer from this location: http://sourceforge.net/projects/mingwbu ... =directory

This is a minimal Web installer that will bring up an install Wizard to guide you through the installation process.

2) Run the installer downloaded from the above step:

Code: Select all

c:\temp\mingw-builds-install.exe
Follow the steps in the wizard taking note of the install folder used. Lets assume the install folder is as shown below:

Code: Select all

C:\Program Files\mingw-builds\x32-4.8.1-posix-dwarf-rev3\mingw32\
Step 3) Once the install is complete use the Windows Control Panel to open up the System icon and use the Environment button found in the Advanced settings to add the bin install folder from the previous step to the PATH environment variable.
system.png
system.png (48.37 KiB) Viewed 919 times
IMPORTANT: Take extreme care when editing this PATH environment variable. Only add to it. Never delete from it.

More details about the PATH can be found here: viewtopic.php?f=5&t=6176

To test the PATH setting changes, start a Windows Command Line Prompt and type in this command line:

Code: Select all

gfortran.exe --version
Running this command line should result in the following output:

Code: Select all

GNU FORTRAN (rev3, Built by MinGW-builds project) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.

GNU FORTRAN comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU FORTRAN
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING
Configuring the Zeus IDE
The information below shows how to run the Fortran compiler from inside the Zeus IDE.

Use the Zeus Options, Document Types menu and edit the FORTRAN Document Type and in the Compiler section define the following details:

Code: Select all

    Command Line: gfortran.exe "$fn" -o "$fdd$fb.exe"
   Errors Regexp: (^Error:)|(\:[0-9]+\.[0-9]+\:$)|(\:[0-9]+[\.0-9]*\:$)
                  (^Error:)|(\:[0-9]+\.[0-9]+\:$)|(\:[0-9]+[\.0-9]*\:$)
 Warnings Regexp: (^Warning:)|(\:[0-9]+\.[0-9]+\:$)|(\:[0-9]+[\.0-9]*\:$)
The command line specified above compile and link the current file and product and executable in the same folder as the source file.

If you only want to syntax check the current file change the command line to be this:

Code: Select all

gfortran.exe -c "$fn"
For more compiler options use the Tools, DOS Command Line menu and type in this command line:

Code: Select all

gfortran.exe --help
A more complete description of the gfortran options can be found here.

Using Modules
FORTRAN allows you to better structure your code by using the USE construct and putting your code into different modules:

Code: Select all

USE random
USE interpolate
...
But if you do use modules then you also need to tell the gfortran.exe compiler where to find those modules.

You can do this using the -Idir as described here, which states:

-Idir

These affect interpretation of the INCLUDE directive (as well as of the #include directive of the cpp preprocessor).
Also note that the general behavior of -I and INCLUDE is pretty much the same as of -I with #include in the cpp preprocessor, with regard to looking for header.gcc files and other such things.


This path is also used to search for .mod files when previously compiled modules are required by a USE statement.

Now if we assume the modules live in a Modules folder relative to the source code, this means the command lines mention earlier become:

Code: Select all

gfortran.exe "$fn" -I.\Modules -o "$fdd$fb.exe"
gfortran.exe -c  -I.\Modules "$fn"
Different Dialects
FORTRAN comes in many dialects and you need to make sure the dialect in use matches the style of code you write.

In the configuration of the command line described earlier, use the -std command line option to set the required FORTRAN dialect.

Below is more information about the different dialects, taken from gfortran page.

Specify the standard to which the program is expected to conform, which may be one of 'f95', 'f2003', 'gnu', or 'legacy'.

The default value for std is 'gnu', which specifies a super-set of the FORTRAN 95 standard that includes all of the extensions supported by GNU FORTRAN, although warnings will be given for obsolete extensions not recommended for use in new code.

The 'legacy' value is equivalent but without the warnings for obsolete extensions, and may be useful for old non-standard programs.

The 'f95' and 'f2003' values specify strict conformance to the FORTRAN 95 and FORTRAN 2003 standards, respectively; errors are given for all extensions beyond the relevant language standard, and warnings are given for the FORTRAN 77 features that are permitted but obsolescent in later standards.

So for example, the following command line will sets the dialect to f95 and turns on the free format code layout:

Code: Select all

gfortran.exe -ffree-form -std=f95 "$fn" -o "$fdd$fb.exe"
To test the compiler settings save the following code to the c:\temp\test.f95 file.

Code: Select all

! Simple FORTRAN Program
program HelloWorld
    write(*,*) "Hello World!"
end program HelloWorld
NOTE: Rather than using the -std dialect option mentioned earlier we use the file extension to define the dialect.

With the c:\temp\test.f95 as the active file use the Compiler, Compile menu to compile the file.

This will produce a c:\temp\test.exe and again use the DOS Command Line menu to run the executable and you should see this output:

Code: Select all

Hello World!
To make it easier to run the executable you could add a tool to the Tools menu or you could use a macro to run the executable.

For example, to run the executable via a macro edit the Macros section of the FORTRAN Document Type and add the the following macro:

Code: Select all

 Menu Text: Execute '$fb.exe'
Macro Name: $zud\zscript\cs_exec.py
 Arguments: $fdd$fb.exe
        [x] Add to popup menu
This will allow you to run the executable using the Macros menu or the Macros tab found in the navigator panel.

NOTE: If the executable runs but does not produce any output refer to the Mysteries of MinGW post for details on how to fix this.

Cheers Jussi