Linux用Windowsサブシステム上のGNUFortranコンパイラ-内部関数を別のプロシージャに渡す際のセグメンテーション違反[重複]

Nov 29 2020

以下は有効なFortran2008プログラムであり、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 Subsystem for 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です()。この問題を解決する方法についての助けは大歓迎です。

回答

3 Rob Nov 30 2020 at 09:25

内部関数を引数として渡すときにセグメンテーション違反の問題を抱えている人として、WSLバージョン2に移行すると問題が解決することがわかりました。