C/C++调用Fortran的使用说明(2)


    2, Fortran 调用 C 函数
    编辑C源码add.c
      1 #include <stdio.h>
      2
      3 void add_(float *a, float *b, float *c)
      4 {
      5         *c = *a + *b;
      6         printf(\"%f + %f = %f\\n\", *a, *b, *c);
      7 }
    Notes The funtion name \"add_\"
    编译源代码
    # gcc -o add.o -c add.c
    编辑fortran 主程序main.f95,编译和执行
      1 PROGRAM MAIN
      2   implicit none [Page]
      3   real:: i, j, k
      4   !
      5   ! The first fortran program
      6   !
      7   i = 5.0
      8   j = 8.0
      9   call add(i, j, k)
     10   stop
     11 end program MAIN
    NOTE 9 line
    # gfortran -o main main.f95 add.o (or f95 -o main main.f95 add.o)
    # ./main
    5.000000 + 8.000000 = 13.000000
 

OK. If you reach here, congratulations, you have learned how to call routines between C and fortran!

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsfyg.html