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

2023-12-29

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


作为有问题的人传递内部函数作为参数时出现分段错误 https://stackoverflow.com/q/49965980/2270574我发现迁移到 WSL 版本 2 可以解决我的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐