[R] Step by step: Making an R package with Fortran 95

Duncan Murdoch murdoch at stats.uwo.ca
Fri May 22 15:37:07 CEST 2009


On 5/22/2009 8:18 AM, sjnovick wrote:
> To all.  I need your help.  The big question is:  How do I make an R library
> with Fortran 95 files?  You may assume that I am a pretty decent programmer
> in both R and Fortran.  I lay out a scenario and need your help!
> 
> I know how to make an ordinary R package and have dabbled with R + Fortran
> 95 *.dll linking.  I do not have a great handle on the whole Makevars file
> and whatever else I might need to get things working to build a new R
> package.  By the way, I'm using R 2.8.1 and Windows XP (and sometimes
> Linux).
> 
> 
> Let's take this simple situation.  Suppose I'm using Windows XP and place
> the Folder for the package "Testme" in C:\Program
> Files\R\R-2.8.1\src\library\Testme

I'd put your own files in their own directory, rather than in the R 
source tree.  But this shouldn't have caused your error...

> Files and folders:
> 
>   DESCRIPTION file -- I understand this file
>   NAMESPACE file:  This one has the contents
>            useDynLib("Testme")
>            exportPattern("^[^\\.]")
> 
>   man directory:  I put my *.Rd files here.  I understand this part.
>   R directory:   I put my *.R files here.  One of these files calls upon a
> Fortran 95 subroutine with
>                              .Fortran("MySubroutine", var1=as.double(x),
> var2=as.double(y), etc.)
>                      I understand this part.
> 
>   src directory:  I put my *.f95 files here.  Also, I put a Makevars file
> here.

What is in Makevars?  You don't need this unless you're doing something 
special.  In your case, you might be, see below.

> 
>       Suppose that I have two *.f95 files, named  CalculatorModule.f95 and 
> ANiceSubroutine.f95.
>   
>               CalculatorModule.f95 contains a Fortran 95 module.
>               ANiceSubroutine.f95 uses the functions in
> Calculator.Module.f95 with the "USE" command.  To be consistent with my R
> file (above), ANiceSubroutine.f95 contains the subroutine: "MySubroutine".
> 
> Finally:  I issue the command from a DOS Shell in the directory C:\Program
> Files\R\R_2.8.1\src\gnuwin32
> 
>             make pkg-Testme
> 
> This results in errors.
> 
> 
> 
> Here are the problems:
>   1.  The order of compilation must be f95 -c CalculatorModule.f95
> ANiceSubroutine.f95.  Unfortunately, R compiles them alphabetically.  So, I
> was given errors making the *.o files.

You could put the dependency

ANiceSubroutine.o:	ANiceSubroutine.f95 CalculatorModule.o

into your Makevars file, but see the instructions in Writing R 
Extensions if you add targets there.

>            
>            I overcame this problem by renaming the files  
> a01.CalculatorModule.f95 and a02.ANiceSubroutine.f95.  I would rather not
> have to name the files this way if I don't have to!  When I did this, R
> compiled the Fortran files in the correct order, but I still have errors.
> 
>    2.  Here was the error output.  Help?????
> 
> C:\Program Files\R\R-2.8.1\src\gnuwin32>make pkg-Testme
> 
> ---------- Making package Testme ------------
>   adding build stamp to DESCRIPTION
>   installing NAMESPACE file and metadata
>   making DLL ...
> "g95"  -O3 -c a01.CalculatorMod.f95 -o a01.CalculatorMod.o
> "g95"  -O3 -c a02.ANiceSubroutine.f95 -o a02.ANiceSubroutine.o
> windres --preprocessor="gcc -E -xc -DRC_INVOKED" -I
> C:/PROGRA~1/R/R-28~1.1/inclu
> de  -i Testme_res.rc -o Testme_res.o
> "g95" -shared -s  -o Testme.dll Testme.def a01.CalculatorMod.o
> a02.ANiceSubroutine.o
> Testme_res.o  -LC:/PROGRA~1/R/R-28~1.1/bin    -lR
> ld: Testme.def:6: syntax error
> ld:Testme.def: file format not recognized; treating as linker script
> ld:Testme.def:2: syntax error
> make[3]: *** [Testme.dll] Error 1
> make[2]: *** [srcDynlib] Error 2
> make[1]: *** [all] Error 2
> make: *** [pkg-Testme] Error 2

Did you create the Testme.def file? What is in it?

Duncan Murdoch

> 
> 
> 
> Thanks for helping!!! :)




More information about the R-help mailing list