ISO_C_BINDING Calling C routine from Fortran (with doubles and arrays)

谁都会走 提交于 2019-11-29 12:53:34

Change your interface to:

module raytracing
  Interface
  integer (C_INT) function photon_trace(BHsp, x_init, x_final) & 
      bind(C, name='photon_trace')  
      use , intrinsic :: ISO_C_BINDING
      implicit none
      real(C_DOUBLE) :: x_init(4), x_final(4)
      real(c_double), value :: BHsp
  end function photon_trace
  end interface
end module raytracing 

Your C function takes a double rather than double* so you need to pass the scalar with the value attribute so that the Fortran knows to pass by value rather than its default pass by reference.

With this small change (and some minor changes to your C to actually print the values of m and phi) this is the output of your example code:

% ./main 
 x_in, x_fin before =    1.0000000000000000        2.0000000000000000        3.0000000000000000        4.0000000000000000        0.0000000000000000        0.0000000000000000        0.0000000000000000        0.0000000000000000     
BHsp 0.700000
t0 1.000000
r0 2.000000
m0 3.000000
phi0 4.000000
t1 2.000000
r1 3.000000
m1 4.000000
phi1 5.000000
 return rt =            0
 x_in, x_fin after =    1.0000000000000000        2.0000000000000000        3.0000000000000000        4.0000000000000000        2.0000000000000000        3.0000000000000000        4.0000000000000000        5.0000000000000000  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!