Basic MinGW Instructions


MinGW is a free suite of programs and utilities for Windows that includes the GNU Fortran Compiler and a utility to make .dll files that you can call from R. The main download is covered by the licensing terms at www.mingw.org.

This page is designed to enable the easy download, installation, and running of MinGW-3.1.0-1. (Running gcc -v should reveal the compiler is gcc version 3.2.3.)


Download & Install | Compiling Fortran | Making DLLs | Credits

External Pages: Quick Guide 1 | A Fortran 77 Manual | About G77 & Index
STAT 740 Spring 2004 Course Page


Download and Installation:

In order to install this on your computer you need to have administrator access to your machine (which you have automatically on your personal machines, but probably not if it is a University owned computer). If you do not normally have administrator access you will want to make sure that you set it so that all users have full access to the MinGW folder on the C drive.

  1. Download the latest executable file: http://prdownloads.sf.net/mingw/MinGW-3.1.0-1.exe?use_default=umn saving it in whatever temporary directory you usually save such files in. (It is about 15mb, so it may take a while on a dial-up connection.)

  2. Run the executable...

  3. Set your computers PATH variable to c:\MinGW\bin;%PATH%
    If there is already another path variable there you add this code to it by separating it from the previous one with a semi-colon (;). I found that it was also necessary to remove a previous PATH entry for a Fortran compiler I no longer used.

  4. Restart your computer so the changes can take place


Compiling Fortran:

To see if it all works we can try compiling and running a short program. At first, anyway, we will store all of our programs in a directory called Programs inside the MinGW directory.

  1. Use either My Computer or Explorer to make a directory called Programs inside the C:\MinGW directory. Then you can close whatever you used to make that directory.

  2. Open either Notepad or WordPad and copy into it the following code1:
      program Convert
      implicit none
      ! -----------------------------------------------Declare
      real*4 tempC, tempF, FACTOR
      integer*2 ZERO_SHIFT
      parameter (ZERO_SHIFT = 32, FACTOR = 5./9.)
      ! -----------------------------------------------Input
      print*, "Enter the temperature in Fahrenheit ..."
      read*, tempF
      ! -----------------------------------------------Compute
      tempC = FACTOR * (tempF - ZERO_SHIFT)
      ! -----------------------------------------------Output
      print*, "The corresponding Centigrade temperature is "
      print*, tempC, " degrees."
      end
    
    and save it as convert.for. Note that you will need to choose the option All Files for Save as Type in Notepad. If you are using WordPad you will need to choose Text Document or something similar.

  3. Choose Run under the Start menu and open command or cmd.

  4. Get to your directory using:

    c:
    cd c:\mingw\programs
    

  5. Type g77 -ffree-form convert.for -o convert.exe

  6. Type convert


Making DLLs:

The following instructions will construct a DLL (a program you can call from R) to raise an entire string of real numbers to some integer power.

  1. Save the following code2 in the c:\MinGW\Programs directory as testit.f :

          subroutine TESTIT(x,n,m)
          dimension x(n)
          do 10 i=1,n
    10      x(i)=x(i)**m
          end
    

    Notice that this is a subroutine and not a function. Therefore it modifies values that were sent to it and returns them (x is changed in this case). In particular the name of that variable has to be included in the subroutine line.

  2. Use the DOS window to go to the c:\MinGW\Programs directory.

  3. Run the following:
    g77 -O2 -c testit.f
    dllwrap --export-all-symbols testit.o -o testit.dll
    

To run this subroutine from R (raising each of the numbers 1 to 5 to the power -2) you would run the following:

dyn.load("c:/MinGw/Programs/testit.dll")
.C("testit_",as.single(1:5),as.integer(5),as.integer(-2))[[1]][1:5]

The first line loads the dll and the second one runs it. Notice that we need to specify what format the subroutine is reading the variables in as. (There is also a .Fortran function but it was behaving oddly in some cases when I tried it out).

A function to call this routine might be a bit easier to use:

testit<-function(x=1.0,m=1){
  if (is.loaded("testit_")==T){
    n<-length(x)
    return(.C("testit_",as.single(x),as.integer(n),as.integer(m))[[1]][1:n])
  }
  else{
    warning("testit.dll was not loaded")
    NULL
  }
}

You would then use testit(1:5,-2) to replicate the above answer.


Credits:

The information and sample programs on this page were pilfered liberally from:

Created: 1/16/04 - B. Habing
Last Updated: 1/17/04 - B.Habing

This page ( www.stat.sc.edu/~habing/courses/740/mingw.html) is maintained by Brian Habing (habing@stat.sc.edu). The views and opinions expressed in this page are strictly those of the page author. The contents of the page have not been reviewed or approved by the University of South Carolina.