- 在main() 主函数中调用fortran 定义的子函数;
基本思想:将fortran定义的函数,在C++函数中采用C接口重新声明为全局函数;为避免C++中函数的重载,通常在声明函数时常使用C的命令extern 声明函数为全局的
a. Accessing Named Common from C and C++
The following example is the Fortran subprogram FCTN called by the previous Cray C main program:
C *********** Fortran subprogram (f.f): ***********
SUBROUTINE FCTN
COMMON /ST/STI, STA(10), STD
INTEGER STI
REAL STA
DOUBLE PRECISION STD
INTEGER I
WRITE(6,100) STI, STD
100 FORMAT ('IN FORTRAN: STI = ', I5, ', STD = ', D25.20)
WRITE(6,200) (STA(I), I = 1,10)
200 FORMAT ('IN FORTRAN: STA =', 10F4.1)
END
The following Cray C main program calls the Fortran subprogram FCTN:
#include <stdio.h>
struct
{
int i;
double a[10];
long double d;
} ST;
main()
{
int i;
/* initialize struct ST */
ST.i = 12345;
for (i = 0; i < 10; i++)
ST.a[i] = i;
ST.d = 1234567890.1234567890L;
/* print out the members of struct ST */
printf("In C: ST.i = %d, ST.d = %20.10Lf\n", ST.i, ST.d);
printf("In C: ST.a = ");
for (i = 0; i < 10; i++)
printf("%4.1f", ST.a[i]);
printf("\n\n");
/* call the fortran function */
FCTN();
}
b.Accessing Blank Common from C or C++
The following example shows how Fortran blank common can be accessed using C or C++ source code:
#include <stdio.h>
struct st
{
float a;
float b[10];
} *ST;
#ifdef __cplusplus
extern "C" struct st *MYCOMMON(void);
extern "C" void FCTN(void);
#else
fortran struct st *MYCOMMON(void);
fortran void FCTN(void);
#endif
main()
{
int i;
ST = MYCOMMON();
ST->a = 1.0;
for (i = 0; i < 10; i++)
ST->b[i] = i+2;
printf("\n In C and C++\n");
printf(" a = %5.1f\n", ST->a);
printf(" b = ");
for (i = 0; i < 10; i++)
printf("%5.1f ", ST->b[i]);
printf("\n\n");
FCTN();
}
This Fortran source code accesses blank common and is accessed from the C or C++ source code in the preceding example:
SUBROUTINE FCTN
COMMON // STA,STB(10)
PRINT *, "IN FORTRAN"
PRINT *, " STA = ",STA
PRINT *, " STB = ",STB
STOP
END
FUNCTION MYCOMMON()
COMMON // A
MYCOMMON = LOC(A)
RETURN
END
Looking for detail information of this type of invoking, you can contact with this web-address:http://docs.cray.com/books/S-2179-52/html-S-2179-52/ppgzmrwh.html
- C++ class functions calling fortran subroutine
C++中的面向对象与Fortran中的面向对象的比较:
http://www1.gantep.edu.tr/~bingul/seminar/f90_c++/comparison.html