C/C++/Fortran混合编程浅谈(一)直接链接方式

     现今流行很多编程语言,在编译型语言中,C/C++/Fortran语言应用非常广泛,C以其效率高效底层操作为著称,C++以其很好的面向对象类框架泛型编程为特点,Fortran则以现世存有大量的计算程序而占有重要的位置,在编程中,集合他们三者的长处是个很好的做法。混合编程有很多方法,以下介绍一下基本方法。

             对于各个编译器,如果编译中间的二进制文件.o或.obj的结构相同,则可以直接链接混合编程。

     遵循约定:C/C++默认传值,Fortran传址。

一、相同编译器家族

以gcc家族为例,类似的还有Intel C Compiler和Intel Fortran Compiler等。

1、C和Fortran

(1)C调用Fortran

main.c

#include <stdio.h>

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;
}

sub.f90

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

sub.f90(F2003方式)

subroutine Sub_Fortran(NumInt,NumFloat,NumDouble)
      use ISO_C_BINDING
      implicit none
      integer(c_int) :: NumInt
      real(c_float) :: NumFloat
      real(c_double) :: NumDouble
      NumDouble=NumFloat**NumInt
end subroutine

real(c_double) function Function_Fortran(NumDouble)
      use ISO_C_BINDING
      implicit none
      real(c_double) :: NumDouble
      Function_Fortran=sqrt(NumDouble)
end function

链接方法

gcc –o main.o –c main.c

gfortran –o sub.o –c sub.f90

gcc –o main.exe main.o sub.o

或者直接  gcc –o main.exe main.c sub.f90

输出

image 

(2)Fortran调用C

main.f90

program main
      implicit none
      interface 
            subroutine sub_c(n1,n2,n3)
                  integer :: n1
                  real :: n2
                  real(8) :: n3
            end subroutine

            real(8) function func_c(n3)
                  real(8) :: n3
            end function
      end interface
      integer :: n1
      real :: n2
      real(8) :: n3,n4
      n1=3
      n2=5.0
      call sub_c(n1,n2,n3)
      n4=func_c(n3)
      write(*,*) "n1=",n1
      write(*,*) "n2=",n2
      write(*,*) "n3=",n3
      write(*,*) "n4=",n4
end program
main.f90(F2003方式)
program main
      use ISO_C_BINDING
      implicit none
      interface 
            subroutine sub_c(n1,n2,n3)
                  use ISO_C_BINDING
                  integer(c_int) :: n1
                  real(c_float) :: n2
                  real(c_double) :: n3
            end subroutine

            real(c_double) function func_c(n3)
                  use ISO_C_BINDING
                  real(c_double) :: n3
            end function
      end interface
      integer(c_int) :: n1
      real(c_float) :: n2
      real(c_double) :: n3,n4
      n1=3
      n2=5.0
      call sub_c(n1,n2,n3)
      n4=func_c(n3)
      write(*,*) "n1=",n1
      write(*,*) "n2=",n2
      write(*,*) "n3=",n3
      write(*,*) "n4=",n4
end program

        sub.c

#include <math.h>

void sub_c_(int *,float *,double *);
double func_c_(double *);


void sub_c_(int *n1,float *n2,double *n3)
{
      *n3=pow(*n2,*n1);
}

double func_c_(double *n3)
{
      double n4;
      n4=sqrt(*n3);
      return n4;
}

链接方式

gcc –o sub.o sub.c

gfortran –o main.o main.f90

gfortran –o main.exe main.o sub.o

或是直接

gfortran –o main.exe main.f90 sub.c

输出

image

posted on 2010-08-25 21:52  PcX  阅读(8806)  评论(0编辑  收藏  举报

导航