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
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.
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.
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." endand 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.
c: cd c:\mingw\programs
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.
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.
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:
Created: 1/16/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.
Last Updated: 1/17/04 - B.Habing