我相信以下是有效的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 OS上。
为了提供更多信息,我注意到将库构建为共享库时,它可以正常工作(即使使用内部函数调用)。但是,它在编译静态库时失败,并显示相同的错误消息。
因此,似乎某些GFortran标志的组合可以以某种方式解决错误(-fPIC -shared
)。非常感谢你提供有关如何解决此问题的帮助。
作为将内部函数作为参数传递时出现分段错误问题的人,我发现转至WSL版本2可以为我解决此问题。
感谢更新。此版本2何时发布?我是最近才安装WSL的,不到两个星期。所以,我应该已经拥有了。
这是输出
uname -a
:Linux ******** 4.4.0-18362-Microsoft #1049-Microsoft Thu Aug 14 12:01:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux
它在几个月前发布,所以我希望您已经安装了它(只要您有更新的Windows 10)。此处有信息docs.microsoft.com/zh-cn/windows/wsl/install-win10,请参阅步骤5,将wsl2设置为默认版本,以防万一您捡到wsl1
谢谢。我可以通过确认它是版本1
wsl -l -v
。我是通过Windows应用商店安装的。那可以解释。