[Rd] using a "third party" DLL in my package

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri May 15 10:23:38 CEST 2009

On Thu, 14 May 2009, Seija Sirkiä wrote:

> Hello all,
> it seems my efforts in reading the manuals and help files aren't enough so 
> here I am. The question is, how would I go about linking a pre-compiled DLL 
> in to my package? I have previously successfully built packages with Fortran 
> and C source code, but now I'd like to take this ready made DLL and call its 
> routines from R.
> My collegue was brave enough to simply try and put the DLL in src folder of 
> the package and proceed with building and installing the same way as if it 
> had been source code. And it works, on my computer and his. Clearly this 
> isn't exactly portable, and while that's ok for the use we have in mind (we 
> both work on Windows XP) I have a nagging feeling this is somehow criminal or 
> unwise at least.

Unwise, since it is unsupported and might stop working at any time. 
Actually, you don't mention the version of R you used and (without 
checking) I think this may not work in all recent versions of R.

What you can so is to have an 'inst/libs' directory in your package 
sources, and put the DLL you want to use there.  Another approach 
(used in a few CRAN packages) is to use 'configure.win' to copy the 
DLL to a 'libs' directory in the installed package.  A third approach 
is to have a 'src/Makefile.win' that creates the DLL you want 
(possibly by just checking it is present in 'src', but also possiby by 
calling the mysterious Fortran compiler X you obliquely mention 

> Why I want to do this might explain it a bit further but in fact the 
> background contains another problem and I welcome anyone to give hints about 
> that too.
> So, we have some Fortran code from way back which deals with fitting taper 
> curves for tree boles, and some other functions related to that. We wanted to 
> make these available for use in R and I made a package with simple wrapper 
> functions for the .Fortran calls, and built it with the help of the compiler 
> in Rtools. All was fine until my collegue managed to bump in to a combination 
> of parameters (tree species, height and breast height diameter) with which 
> the computation freezes. We traced it to a certain very simple iteration in 
> the Fortran code that finds the height at which the tree has a given 
> diameter. I could give more details on that, but the point is that the very 
> same computation goes through just fine when executed "fully" in Fortran, 
> with the routines and an interface compiled with another Fortran compiler. 
> And also when that compiler is used to make a DLL and that DLL is used within 
> the R package in the way I described above.
> In summary, what we seem to have here is a compiler dependent convergence 
> problem. Possible solutions are to figure out what's wrong with the 
> computation - and I've pretty much exhausted my skills on that - or to figure 
> out how to use a working DLL, as was my first question.

My concern would be that there are different cases that fail under 
Fortran compiler X and you are just sweeping the problem under the 

> Great big thanks in advance to anyone with advice!
> Seija Sirkiä,
> senior researcher (statistics), Finnish forestry research institute

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-devel mailing list