[R] Underscores and Fortran code

Duncan Temple Lang duncan at research.bell-labs.com
Fri Oct 12 16:28:28 CEST 2001


And a 6th way to approach this is to use the mechanism for registering
native routines with R. This allows a shared library to explicitly
tell R what routines can be called by users using the .Fortran(),
.C(), .Call() and .External() functions, and it allows you to
associate alias names for each routine.  Then you can use those names
in the .Fortran() call, rather than the actual name.  This avoids the
entire dynamic lookup and the Fortran underscore problem.

A quick example might help. Suppose we want to create a shared library
named "myLib" and it contains one Fortran routine, named "__doit_".
Rather than use .Fortran("__doit"), suppose we want to call it
as .Fortran("myRoutine"). Then we can create the following
routine in our shared library and R will take care of 
registering the routines and mapping 
       .Fortran("myRoutine")
to a call to __doit_().

void R_init_myLib(DllInfo *dll)
{
  R_FortranMethodDef fortranRoutines[] = {
    { "myRoutine", (DL_FUNC) &__doit_, -1 },
    NULL
  };
    R_registerRoutines(dll, NULL, NULL, fortranRoutines, NULL);
}

(I can't recall off-hand about whether the you should use (DL_FUNC)
&__doit_ or (DL_FUNC) &__doit in the array definition. But the
compiler will let you know very quickly.)

Then when the library is loaded, .Fortran("myRoutine", ......) should
work just fine.

Each of the libraries in the R distribution uses this registration
mechanism and you can look there for examples. (Unfortunately none
contain Fortran examples.) Additionally, there is a short description
in the upcoming R newsletter and a slightly more cryptic description
available at
  http://cm.bell-labs.com/stat/duncan/DynamicCSymbols.ps

This may or may not be easier than any of the other 5 approaches
mentioned previously.  It is probably the least intrusive and most
portable of all however, so is probably worth it if you can recompile
the shared library (or link it into a new one).

Please let me know if there are any problems if you do use it. 

 D.



Duncan Murdoch wrote:
> On Thu, 11 Oct 2001 16:48:12 -0400, you wrote in message
> <15302.1420.571215.515265 at minke.stat.ufl.edu>:
> 
> >Any suggestions are welcome.  Am I missing anything?
> 
> A 5th suggestion:  you can change the R code.  In the Windows version
> 1.3.1, I get these results:
> 
> Just like you, an error with the real name:
> 
>  > .Fortran("dog_dog", as.double(x), as.double(y), as.double(z))
>  Error in .Fortran("dog_dog", as.double(x), as.double(y),
> as.double(z)) : 
>          C/Fortran function name not in load table
> 
> But add an extra underscore to the call and things are fine:
> 
>  > .Fortran("dog_dog_", as.double(x), as.double(y), as.double(z))
>  [[1]]
>  [1] 2
> 
>  [[2]]
>  [1] 3
> 
>  [[3]]
>  [1] 5
> 
> It seems like a fairly easy workaround to just append an extra
> underscore to names when the compiler is doing this funny name
> mangling.
> 
> Duncan Murdoch
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
_______________________________________________________________

Duncan Temple Lang                duncan at research.bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-3217
700 Mountain Avenue, Room 2C-259  fax:    (908)582-3340
Murray Hill, NJ  07974-2070       
         http://cm.bell-labs.com/stat/duncan
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list