[R] Underscores and Fortran code

Peter Dalgaard BSA
Thu, 11 Oct 2001 22:01:17 -0000

Brett Presnell <presnell at stat.ufl.edu> writes:

> 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?

Probably not. I've certainly never heard of that convention before.
Given that you're not likely to be mixing your code with things
compiled with other compilers, option 2 does look viable. However,
your code might become nonportable to non-g77 compilers if they have
the convention and no way to turn it off.

For option 3, you might change the code in do_dotCode, but how would
you replace

# define F77_CALL(x)    x ## _

with something that checks for underscores in x? That does not look
easy.

Which leaves option 1....


There is a 4th possibility: Write your own C wrappers for the Fortran
functions and have them conditionalize properly. If you figure out how
to test for the property in configure, we might be convinced to add
the definition of (say) F77_2ND_UNDERSCORE to the relevant include
file.

--
   O__  ---- Peter Dalgaard             Blegdamsvej 3
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907