[R] Calling Fortran from C++
William Dunlap
wdunlap at tibco.com
Mon Jun 1 18:51:53 CEST 2009
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Thomas Lumley
> Sent: Monday, June 01, 2009 9:33 AM
> To: cls59
> Cc: r-help at r-project.org
> Subject: Re: [R] Calling Fortran from C++
>
> On Mon, 1 Jun 2009, cls59 wrote:
>
> >
> >
> > Giura Gauss wrote:
> >>
> >> Hi,
> >>
> >> can anybody point me to a package with C++ code that call Fortran
> >> subroutines?
> >>
> >> I am trying to do the same thing but we scarce success.
> >>
> >> Error in dyn.load("utils.so") :
> >> unable to load shared library 'utils.so':
> >> dlopen(utils.so, 6): Symbol not found: _robcovf
> >> Referenced from: utils.so
> >> Expected in: dynamic lookup
> >>
> >>
> >>
> >
> > It seems like you have a problem with function names which
> is a common
> > obstacle when interfacing C and Fortran. What usually
> happens is that a
> > trailing underscore gets added to fortran function names
> when they are
> > compiled. For example, consider a fortran subroutine declared as:
> >
> > subroutine gaussQuad ( a, b, f1, f2 )
> >
> > Since an underscore usually gets added and Fortran passes
> variables by
> > reference, the above subroutine would be seen by C as:
> >
> > void gaussQuad_( a&, b&, f1&, f2& );
> >
>
> Because this varies from compiler to compiler, R provides
> macros that do the correct name mangling, so it would be
> void F77_CALL(*a, *b, *f1, *f2)
> in C (not a&, b&, etc, which are C++ syntax).
>
> -thomas
>
> Thomas Lumley Assoc. Professor, Biostatistics
> tlumley at u.washington.edu University of Washington, Seattle
In addition, Fortran subroutines with character or logical arguments
may give you trouble on various platforms; avoid such arguments
in Fortran code you plan on calling from C/C++. When Fortran passes
a character argument it also passes its length. Some Fortran compilers
put the length at the end of the argument list and some put it right
after the character argument. Some Fortran compilers encode the
logical .FALSE. as -1, some as 0, with .TRUE. being anything but
.FALSE. I've seen one Fortran compiler that encoded .TRUE. as
the 8th bit from the left being 1 and .FALSE. as that bit being 0
(all other bits are ignored).
Unfortunately Lapack routines tend to have a character argument,
making them a pain to portably call from C/C++.
Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com
More information about the R-help
mailing list