我相信以下是一个有效的 Fortran 2008 程序,它可以在使用 Intel 和 GNU Fortran 编译器的正版 macOS、Linux 和 Windows 操作系统上正常运行。
module InternalFuncCaller_mod
implicit none
abstract interface
function getInternalFunc_proc(input) result(output)
implicit none
real, intent(in) :: input
real :: output
end function getInternalFunc_proc
end interface
contains
subroutine callInternalFunc(getInternalFunc, x)
implicit none
procedure(getInternalFunc_proc) :: getInternalFunc
real, intent(in) :: x
write(*,*) getInternalFunc(x)
end subroutine callInternalFunc
end module InternalFuncCaller_mod
module InternalFunc_mod
implicit none
contains
subroutine passInternalFunc()
use InternalFuncCaller_mod, only: callInternalFunc
implicit none
call callInternalFunc(getThisInternalFunc, x = 4.)
contains
function getThisInternalFunc(x) result(sqrtx)
implicit none
real, intent(in) :: x
real :: sqrtx
sqrtx = sqrt(x)
end function getThisInternalFunc
end subroutine passInternalFunc
end module InternalFunc_mod
program testInternalFuncCall
use InternalFunc_mod
implicit none
call passInternalFunc()
write(*,*) "Done."
end program testInternalFuncCall
但是,当在 Windows Linux 子系统 (WSL) (Ubuntu) 上使用 GFortran 进行编译并运行时,它会给出以下 SegFault 错误消息:
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0x7ffb84580d3a
#1 0x7ffb8457fed5
#2 0x7ffb843a620f
#3 0x7fffde946cb0
Segmentation fault (core dumped)
我已将问题追溯到外部过程的内部函数调用。但相同的代码在具有不同 Fortran 编译器的所有其他操作系统上都可以正常工作。因此,这似乎不是 GNU GFortran 的错误,而更可能是静态编译和执行包含对另一个过程的内部过程的外部调用的代码的问题,特别是在 WSL 操作系统上。
为了提供更多信息,我注意到当该库构建为共享库时,该库工作正常(即使使用内部函数调用)。但是,在编译静态库时,它会失败并显示相同的错误消息。
因此,一些 GFortran 标志的组合似乎可以以某种方式解决该错误(-fPIC -shared
)。非常感谢任何有关如何解决此问题的帮助。