【发布时间】:2018-10-02 14:35:06
【问题描述】:
我有一些代码将主程序的内部函数作为参数传递给函数:当传递的函数最终被调用时,它会导致分段错误。这只发生在我使用适用于 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 上的一些非标准功能?
【问题讨论】:
-
也许一些指针:stackoverflow.com/questions/40696534/… 和谷歌搜索“fortran 包含函数的范围”
-
在 Fortran 2008 下允许内部过程(例如包含在主程序中的
f)作为实际参数,这在 gfortran 7 中实现。然后发生了其他事情。 -
在我放弃 WSL 之前,我正在构建 gcc/g++/gfortran(由 WSL gcc 引导)以获得当前版本。尽管如此,WSL 还是有太多问题,所以我回到 cygwin 在 Windows 上进行 gfortran。
-
我确实尝试了从 WSL 上的源代码构建的最新 svn 版本的 gfortran,但它仍然是一个问题。还在本机 linux 上尝试了 ifort 18 并且该测试通过了,所以似乎是 WSL 问题。
-
截至 2020 年 11 月仍然存在问题。请参阅此页面:stackoverflow.com/questions/65066837/…
标签: fortran gfortran windows-subsystem-for-linux