Use Fortran subroutine in R? Undefined symbol

前端 未结 3 1749
暗喜
暗喜 2020-12-12 02:03

This is a follow up to my previous question. I wrapped my Fortran code in a module and now it compiles when I run:

R CMD SHLIB ./Fortran/Fpi.f90
3条回答
  •  無奈伤痛
    2020-12-12 02:22

    Your problem comes down to the declaration of dboard:

       double precision                ::  pi_est, homepi, pirecv, pisum, dboard
    

    Here you are saying that dboard is an external function, rather than a module procedure. This explains why there is a symbol dboard_ coming into play. You want to remove that:

    double precision                ::  pi_est, homepi, pirecv, pisum
    

    and instead rely, in pi on the module procedure-ness of dboard: pi already knows about it without this declaration.

    Now, beyond that, because pi is in a module there is going to be some name mangling going on for that subroutine itself. I'd solve this problem by making pi itself a (C) interoperable procedure.

    Module Fpi 
      IMPLICIT NONE
    contains 
      subroutine pi(avepi, DARTS, ROUNDS) bind(C)
        use, intrinsic :: iso_c_binding, only : c_double, c_int
        real(c_double), intent(out)   ::  avepi
        integer(c_int), intent(in)    ::  DARTS, ROUNDS
        ...
    

    and then using .C rather than .Fortran.

    You can keep pi and dboard in the module, and this latter needn't even be interoperable.

提交回复
热议问题