[R] Calling Fortran from C++

0 views
Skip to first unread message

Giuseppe

unread,
Jun 1, 2009, 5:18:35 AM6/1/09
to r-h...@r-project.org
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

[[alternative HTML version deleted]]

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

cls59

unread,
Jun 1, 2009, 11:58:19 AM6/1/09
to r-h...@r-project.org

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& );

Furthermore, in C++ I think you h$ave to wrap the prototype in an extern
statement:

extern "C" { void gaussQuad_(a&, b&, f1&, f2& ); }

If you are running a unix system, you can check if your fortran function
names are receiving trailing underscores by running the nm utility on your
shared library. Ignore the leading underscores as they are expected by the C
compiler.

-Charlie


-----
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
--
View this message in context: http://www.nabble.com/Calling-Fortran-from-C%2B%2B-tp23812140p23817336.html
Sent from the R help mailing list archive at Nabble.com.

Thomas Lumley

unread,
Jun 1, 2009, 12:33:02 PM6/1/09
to cls59, r-h...@r-project.org
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
tlu...@u.washington.edu University of Washington, Seattle

William Dunlap

unread,
Jun 1, 2009, 12:51:53 PM6/1/09
to Thomas Lumley, cls59, r-h...@r-project.org

> From: r-help-...@r-project.org
> [mailto:r-help-...@r-project.org] On Behalf Of Thomas Lumley
> Sent: Monday, June 01, 2009 9:33 AM
> To: cls59
> Cc: r-h...@r-project.org
> Subject: Re: [R] Calling Fortran from C++

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

Giuseppe

unread,
Jun 1, 2009, 7:46:01 PM6/1/09
to r-h...@r-project.org
> From: r-help-...@r-project.org
<http://tolstoy.newcastle.edu.au/R/e6/help/09/03/8794.html#16298qlink1>
*> [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


Thank you all. I should have known about F77_CALL. I think I used once.

However, I have still problems.

robcovf.f constains a fortran subroutine: subroutine robcovf(n, p, nc,
start, len, u, s, v, w)

My C++ file (utils.cc):

extern "C"{
void robcovf_(const int *n, const int *p, const int *nc,
const int *start, const int *len,
double *u, double *s, double *v, double *w);

void wb(const double* Xdata, const double* Ydata,
const int* nrow, const int* ncol, const int* nrep,
const int* clusstart, const int* clussize, const int* nc,
double* out1, double* out2)
{

[...]

F77_CALL(robcovf)(nrow, ncol, nc, clusstart, clussize, u, s, v, w);

[other stuff...]
}


I do:

R CMD SHLIB robcovf.f

**R CMD SHLIB utils.cc

They compile just fine, but

> dyn.load('utils.so')


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


Now, I have two underscores!!


*
*

*

[[alternative HTML version deleted]]

Reply all
Reply to author
Forward
0 new messages