Windows Linux 子系统上的 GNU Fortran 编译器 - 将内部函数传递给另一个过程时出现分段错误

GNU Fortran compiler on Windows Subsystem for Linux - Segmentation fault with passing internal function to another procedure

我相信以下是一个有效的 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 可以解决我的问题。