Calling Fortran subroutines from C++

Dan, Ho-Jin hjdan@kaist.ac.kr
Fri Oct 26 18:56:00 GMT 2001


for example,
--in a fortran code, fcode.f
subroutine fsubr(m,x,y)
integer m
double x(*),y(*)
.....snip....

--in a cplus code cccallsf.cc
#include "g2c.h" // see f2c
extern "C" void fsubr_(inteter*m, realdouble*x, realdouble*y); // call 
by ref
// you can call fsubr_() without including g2c.h. integer -> int , 
realdouble -> double
...snip
int main()
{
integer m[1] ;
doublereal x[10], y[10];
*m = 10;
fsubr_(m,x,y);
...
}

$g77 -c fcode.f
$g++ -c cccallsf.cc
$g++ -o cccallsf cccallsf.o fcode.o -lg2c -lm

Daniela De Angelis wrote:

>I am interested in calling Fortran Subroutines from C++. Apart from the
>declaration of the Fortran
>function within my program, which I think I have sorted out, I wonder
>whether there is
>a compiling option that tells the compiler that the library I am using
>is a Fortran Library.
>I have looked for clues on the Web, with no success. I wonder whether
>you can help.
>
>Thanks
>
>--
>
>Daniela De Angelis
>MRC Biostatistics Unit                          Tel: + (0)1223 330390
>Institute of Public Health
>Robinson Way                                    Fax: + (0)1223 330388
>Cambridge
>CB2  2SR                                        Web: www.mrc-bsu.cam.ac.uk
>United Kingdom
>
>
>
>
>





More information about the Gcc-help mailing list