Warm tip: This article is reproduced from serverfault.com, please click

GNU Fortran compiler on Windows Subsystem for Linux-将内部函数传递给另一个过程的分段错误

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

发布于 2020-11-30 00:56:21

我相信以下是有效的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)。非常感谢你提供有关如何解决此问题的帮助。

Questioner
King
Viewed
11
Rob 2020-11-30 17:25:21

作为将内部函数作为参数传递时出现分段错误问题的人,我发现转至WSL版本2可以为我解决此问题。