适用于Linux的Windows子系统上的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操作系统上都可以正常工作.
I believe the following is a valid Fortran 2008 program and it works fine on genuine macOS, Linux, and Windows operating systems with both Intel and GNU Fortran compilers.
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子系统(WSL)(Ubuntu)上使用GFortran编译并运行时,它会显示以下SegFault错误消息:
However, when compiled with GFortran on a Windows Subsystem for Linux (WSL) (Ubuntu) and run, it gives the following SegFault error message:
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上.
I have traced the issue to the internal function call by the external procedure. But the same code works fine on all other Operating Systems with different Fortran compilers. So, this does not appear to be a bug with GNU GFortran, but more likely an issue with static compilation and execution of code that contains external calls to internal procedures of another procedure, in particular, on WSL OS.
要提供更多信息,我注意到该库作为共享库构建时可以正常工作(即使使用内部函数调用).但是,在编译静态库时,它会失败,并显示相同的错误消息.
To give more information, I have noticed that the library works fine (even with internal function calls) when it is built as a shared library. However, it fails with the same error message when compiled a static library.
因此,似乎某些GFortran标志的组合可以以某种方式解决错误( -fPIC -shared
).非常感谢您提供有关解决此问题的帮助.
So, it seems like a combination of some GFortran flags can somehow resolve the error (-fPIC -shared
). Any help on how to resolve this problem is greatly appreciated.
推荐答案
作为传递内部函数时出现分段错误的出现问题的人作为论据我发现转用WSL版本2可以解决此问题.
As the person with the problem in Segmentation fault when passing internal function as argument I've found moving to WSL version 2 fixes the issue for me.
这篇关于适用于Linux的Windows子系统上的GNU Fortran编译器-将内部函数传递给另一个过程的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!