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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 12 Oct 2001, 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.BTW, _ is not valid in an ISO Fortran 77 name, so this is all about compiler-specific extensions. On Solaris f77 does not add, g77 does. We could make .Fortran do this automatically .... (I mean try dog_dog_ then dog_dog__) However F77_CALL etc would still be wrong, and I am inclined to suggest we do adopt -fno-second-underscore with g77. Or advise people to use standard-conforming Fortran 77 .... -- 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 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._