我有一些代码将主程序的内部函数作为参数传递给函数:当传递的函数最终被调用时,它会导致分段错误。只有当我在Linux上使用Windows子系统(我在WSL上使用Ubuntu 16 )时才会发生这种情况;在本地Linux或Mac机器上运行时不会发生这种情况。
一个最小的崩溃示例:
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移动到它自己的模块中,但仍然作为参数传递),所以程序中包含了一些关于f的内容。
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
module test2
implicit none
contains
real function f(y)
real, intent(in) :: y
write(*,*) y
f=y*y
end function f
end module test2
program tester
use test1
use test2
implicit none
call x(f,1.0)
end program tester
gfortran-7 -ggdb test_fun_passing.f90 -o test && ./test
1.00000000 以这种方式传递f是有效的Fortran,还是我在本地Linux上转发了一些非标准特性?
发布于 2018-04-25 14:38:21
看来我会遇到这样的情况:
https://github.com/Microsoft/WSL/issues/3083和https://github.com/Microsoft/WSL/issues/286
WSL有一个不可执行的堆栈.跑步:
excestack -c test在本机linux上,要从二进制文件中删除导出堆栈,就会触发与我在WSL上相同的错误消息。清除/设置WSL上的外挂堆栈(带有-c/-s)什么也不做。从github bug报告来看,它似乎不太可能被修复。
编辑:
似乎转移到WSL版本2解决了这个问题。
https://stackoverflow.com/questions/49965980
复制相似问题