Fortran 将内部函数作为参数传递时出现分段错误
我有一些代码将主程序的内部函数作为参数传递给函数:当传递的函数最终被调用时,它会导致分段错误。这只有在我使用Linux的Windows子系统时才会发生(我在WSL上使用的是Ubuntu 16);在本机Linux或Mac计算机上运行时,不会发生这种情况 崩溃的最小示例:Fortran 将内部函数作为参数传递时出现分段错误,fortran,gfortran,windows-subsystem-for-linux,Fortran,Gfortran,Windows Subsystem For Linux,我有一些代码将主程序的内部函数作为参数传递给函数:当传递的函数最终被调用时,它会导致分段错误。这只有在我使用Linux的Windows子系统时才会发生(我在WSL上使用的是Ubuntu 16);在本机Linux或Mac计算机上运行时,不会发生这种情况 崩溃的最小示例: module test1 implicit none contains subroutine x(ff,y) interface real function ff(y) r
module test1
implicit none
contains
subroutine x(ff,y)
interface
real function ff(y)
real, intent(in) :: y
end function ff
end interface
real, intent(in) :: y
integer z
z=ff(y)
end subroutine x
end module test1
program tester
use test1
implicit none
call x(f,1.0)
contains
real function f(y)
real, intent(in) :: y
write(*,*) y
f=y*y
end function f
end program tester
汇编时使用:
gfortran-7 -ggdb test_fun_passing.f90 -o test
回溯、gdb输出:
(gdb) bt
#0 0x00007ffffffde320 in ?? ()
#1 0x0000000000400734 in test1::x (ff=0x7ffffffde320, y=1) at test_fun_passing.f90:17
#2 0x0000000000400829 in tester () at test_fun_passing.f90:31
#3 0x0000000000400860 in main (argc=1, argv=0x7ffffffde64f) at test_fun_passing.f90:27
#4 0x00007ffffec70830 in __libc_start_main (main=0x40082c <main>, argc=1, argv=0x7ffffffde448, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffffffde438) at ../csu/libc-start.c:291
#5 0x0000000000400669 in _start ()
以这种方式传递
f
是有效的Fortran,还是我一直在使用本机Linux上的一些非标准功能?看起来我遇到了以下问题:
及
WSL有一个不可执行的堆栈。运行:
excestack -c test
在本机linux上,要从二进制文件中删除execstack,会触发与我在WSL上相同的错误消息。在WSL上清除/设置execstack(使用-c/-s)没有任何作用。从github bug报告来看,它似乎不太可能被修复
编辑:
移动到WSL版本2似乎解决了这个问题可能有一些提示:谷歌搜索“fortran包含函数的范围”在fortran 2008中允许内部过程(例如主程序中包含的
f
)作为实际参数,这在gfortran 7中实现。在我放弃WSL之前,我正在构建gcc/g++/gfortran(由WSLCCC引导)以获得当前版本。即便如此,WSL还是有太多问题,所以我回到了cygwin for gfortran Windows上。我确实尝试了最新的svn版本的gfortran,它是从WSL上的源代码构建的,这仍然是一个问题。也在本机linux上尝试了ifort 18,并且该测试通过,因此似乎是WSL问题。截至2020年11月,仍然是一个问题。请参阅本页:是的,内部函数通常使用蹦床传递,因此需要可执行堆栈。
excestack -c test