[Rd] Shared routines in a dynamic library.

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jul 30 22:36:06 CEST 2012

On 30/07/2012 14:48, David L Lorenz wrote:
> All,
>    I have a question about referring to a subroutine that is in a dynamic
> library other than the one in my current package.
> Let's say that I have a package foo and it has some FORTRAN code that
> contains the subroutines foo1 and foocommon. The package foo has an R
> function that calls foo1 and foocommon is a subroutine that is called from
> foo1.
> And, I have another package called bar which also has some FORTRAN code
> that contains the subrouting bar1. But bar1 also calls foocommon. The bar
> package is distinctly different from foo, but would require foo (even if
> the FORTRAN code did not have the common subroutine).
>    I can think of a few ways to resolve this.
> 1. Place a copy of foocommon.f into the shared library for bar.
> 2. Put all of the FORTRAN code into the foo package and just rely on the R
> code in bar to call the code that it needs.
> 3. Refer to the shared library in the bar package. I assume that I'd do
> this in the NAMESPACE file using useDynLib(foo) in addition to
> useDynLib(bar), but I found no reference to it in Writing R extensions.
>    Is there a recommended way to solve this issue? Thanks.

.  It is the main recommendation (and none of the above).

If bar loads foo, you can access things in foo's DLL by e.g.
.C("bah", PACKAGE="foo").  foo's DLL must be loaded so (3.) is not 
relevant.   But that is dangerous (foo might change) and will be noted 
by R CMD check.

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