使用gfortran编译Fortran代码遇'rfunct接口需显式定义'错误求助
解决gfortran编译错误:Interface of ‘rfunct’ must be explicit
错误原因
gfortran遵循更严格的Fortran标准,在bind(c)的子程序接口中,不能仅通过external声明函数指针,必须提供显式的函数接口定义。旧写法能在g95通过,但不符合现代Fortran规范。
修复方案
步骤1:定义显式的函数指针接口类型
先通过abstract interface定义与rfunct匹配的函数接口类型,明确返回值类型和C绑定属性:
use, intrinsic :: iso_c_binding implicit none ! 定义C绑定的函数指针抽象接口 abstract interface function rfunct_proto() bind(c) import :: c_double real(kind=c_double) :: rfunct_proto end function rfunct_proto end interface
步骤2:修改子程序接口
在ifunct的接口中,用procedure(rfunct_proto)声明函数指针参数,替代原来的external声明:
interface subroutine ifunct(rfunct) bind(c) import :: c_double, rfunct_proto ! 用procedure(接口类型)声明函数指针参数 procedure(rfunct_proto), intent(in) :: rfunct end subroutine ifunct end interface ! 绑定到实际调用的setvalue子程序 procedure(ifunct), bind(c, name='setvalue') :: setvalue
步骤3:确保实际函数类型匹配
检查rang的定义,保证其返回值类型与c_double一致(即R8需等价于c_double),建议显式关联:
! 在rang所在的模块中 use, intrinsic :: iso_c_binding integer, parameter :: R8 = c_double public :: rang contains function rang() result(res) real(R8) :: res ! 函数逻辑实现 end function rang
验证修改
修改后重新用gfortran编译,即可解决显式接口缺失的错误。
内容的提问来源于stack exchange,提问作者Junaid Arshad
相关产品推荐
相关产品推荐

