[R] Underscores and Fortran code

Brett Presnell presnell at stat.ufl.edu
Thu Oct 11 22:48:12 CEST 2001


This might more properly be filed as a bug report, but ...

I came upon the following problem while trying to dyn.load a library
of Fortran code into R.  I'm running RedHat 7.1 on a Pentium III
laptop, with R version 1.3.1 (latest rpm from CRAN) and gcc/g77
version 2.96.

My library has a number of Fortran subroutines that have underscores
in their names for readability.  By default g77 appends TWO
underscores to the ends of Fortran names that contain an underscore.
According to the g77 documentation this is for compatibility with f2c
and "many other UNIX Fortran compilers".  Unfortunately these symbols
are not in turn found by R, which expects only a single underscore in
all cases.

I can think of three obvious solutions to this problem:

1.  I could edit all my code and take the underscores out of the
    names.

    I definitely don't want to do this.

2.  Add -fno-second-underscore to FFLAGS in ${R_HOME}/etc/Makeconf.

    This seems to work for me, though I'm concerned that it might
    break something else down the line.  In general, I suppose that
    this could be done in configure, though I don't know which other
    compilers have this feature, or what switches they might use to
    turn off it off.

3.  Change symbol.For, .Fortran, dyn.load, and whatever else to
    produce/look for the correct names.

    This seems like the "right" answer, though it would presumably
    require some work in configure and elsewhere.  I notice that there
    is already some underscore checking going on in configure and in
    places like Rdynload.c.

Any suggestions are welcome.  Am I missing anything?

To save typing for anyone that want's to try this at home, I've
appended a simple test case below.

-- 
Brett Presnell
Department of Statistics
University of Florida
http://www.stat.ufl.edu/~presnell/

dog1.f:

      subroutine dog_dog(x,y,z)
      implicit none
      double precision x,y,z

      z = x+y
      return
      end

dog2.f:

      subroutine dogidog(x,y,z)
      implicit none
      double precision x,y,z

      z = x+y
      return
      end

After

bash$ R CMD SHLIB dog1.f
bash$ R CMD SHLIB dog2.f

note the symbols

bash$ nm dog1.so | grep dog
00000730 T dog_dog__
bash$ nm dog2.so | grep dog
00000730 T dogidog_

and in R

> x <- 2; y <- 3; z <- 0
> dyn.load("dog1.so")
> .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
> dyn.unload("dog1.so")
> dyn.load("dog2.so")
> .Fortran("dogidog", as.double(x), as.double(y), as.double(z))
[[1]]
[1] 2

[[2]]
[1] 3

[[3]]
[1] 5

> dyn.unload("dog2.so")
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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