在C语言中调用fortran(c和fortran混合编程)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

#include 
void sub_fortran_(int *,float *,double *);
double function_fortran_(double *);
int main()
{
      int num_int;
      float num_float;
      double num_double;
      double num;
      num_int=3;
      num_float=5.0;

      sub_fortran_(&num_int,&num_float,&num_double);
      num=function_fortran_(&num_double);
      printf("num_int=%d\nnum_float=%f\nnum_double=%f\nnum=%f",num_int,num_float,num_double,num);
      return 0;
}

fortran代码:

subroutine Sub_Fortran(NumInt,NumFloat,NumDouble)
      implicit none
      integer :: NumInt
      real :: NumFloat
      real(8) :: NumDouble
      NumDouble=NumFloat**NumInt
end subroutine

real(8) function Function_Fortran(NumDouble)
      implicit none
      real(8) :: NumDouble
      Function_Fortran=sqrt(NumDouble)
end function

编译:

gcc -o main.o -c main.c    
gfortran -o sub.o -c sub.f90
gcc -o main main.o sub.o -lm 

运行结果:

$ ./main 
num_int=3
num_float=5.000000
num_double=125.000000

 

转载于:https://my.oschina.net/u/2245781/blog/803790

你可能感兴趣的:(在C语言中调用fortran(c和fortran混合编程))