(1) 处的外部函数“f”在带有 f2py 的子例程中没有 IMPLICIT 类型

External function ‘f’ at (1) has no IMPLICIT type in subroutine with f2py

我使用 Fortran 90 有一段时间了,最​​近决定使用 f2py 来封装一些模块,通过使用 Python 作为前端来简化原型设计。

但是,我在尝试编译传递给外部函数(来自 Python)的子例程时遇到错误。这是复制错误的代码:

! file: callback.f90
subroutine callback(f,x,y)
  implicit none
  ! - args -
  ! in
  external :: f
  real(kind=kind(0.0d0)), intent(in) :: x
  ! out
  real(kind=kind(0.0d0)), intent(inout) :: y

  y = f(x)
end subroutine

现在,如果我使用 f2py 生成签名文件 (.pyf),这是我获得的:

!    -*- f90 -*- 
! Note: the context of this file is case sensitive.
python module callback__user__routines 
interface callback_user_interface 
    function f(x) result (y) ! in :callback:callback.f90
        intent(inout) f
        real(kind=kind(0.0d0)) intent(in) :: x
        real(kind=kind(0.0d0)) intent(inout) :: y
    end function f
end interface callback_user_interface
end python module callback__user__routines
python module callback ! in  
    interface  ! in :callback
        subroutine callback(f,x,y) ! in :callback:callback.f90
            use callback__user__routines
            external f
            real(kind=kind(0.0d0)) intent(in) :: x
            real(kind=kind(0.0d0)) intent(inout) :: y
        end subroutine callback
    end interface 
end python module callback

! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/

这是不正确的,所以我按以下方式修改它:

!    -*- f90 -*- 
! Note: the context of this file is case sensitive.
python module __user__routines
    interface
        function f(x) result (y) 
            real(kind=kind(0.0d0)) intent(in) :: x
            real(kind=kind(0.0d0)) :: y
        end function f
    end interface
end python module __user__routines
python module callback ! in  
    interface  ! in :callback
        subroutine callback(f,x,y)
            use __user__routines
            external f
            real(kind=kind(0.0d0)) intent(in) :: x
            real(kind=kind(0.0d0)) intent(inout) :: y
        end subroutine callback
    end interface 
end python module callback

! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/

尽管如此,两者都抛出一个错误告诉我我没有定义 f:

callback.f90:1:21:
subroutine callback(f,x,y)
                    1
Error: Symbol ‘f’ at (1) has no IMPLICIT type
callback.f90:10:6:

  y = f(x)
      1
Error: Can't convert UNKNOWN to REAL(8) at (1)
callback.f90:1:21:

subroutine callback(f,x,y)
                    1
Error: Symbol ‘f’ at (1) has no IMPLICIT type
callback.f90:10:6:

   y = f(x)
       1
Error: Can't convert UNKNOWN to REAL(8) at (1)

我已经尽可能多地研究手动正确编辑 .pyf 签名文件,但在这种情况下无济于事。有没有人在尝试将 f2py 与外部函数一起使用时遇到类似的错误?

错误是正确的,它是一个 Fortran 错误,即使没有任何 f2py 或 Python

也会发生
subroutine callback(f,x,y)
  implicit none
  external :: f
  real(kind=kind(0.0d0)), intent(in) :: x
  real(kind=kind(0.0d0)), intent(inout) :: y

  y = f(x)
end subroutine

f 没有声明类型并且 implicit none 被启用所以这是一个 Fortran 错误。

使用

  real(kind=kind(0.0d0)), external :: f

  external :: f
  real(kind=kind(0.0d0)) :: f