[R] Calling Fortran from C++

Thomas Lumley tlumley at u.washington.edu
Mon Jun 1 18:33:02 CEST 2009


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




More information about the R-help mailing list