重复调用的Fortran从C函数时分段故障 [英] Segmentation fault when calling a C function from Fortran repeatedly
问题描述
我有一个Fortran 90的程序,在C函数重复调用。第一次函数用于一切顺利,但第二次code试图访问它提供了以下错误功能:
I have a Fortran 90 program which repeatedly calls on a C function. The first time the function is used all goes well, but the second time the code tries to access the function it gives the following error:
计划接收信号SIGSEGV:段错误 - 无效的内存引用。
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
我在Windows上使用gfortran v.4.6,链接,而我没有源$ C $ C库。图书馆来找我作为一个DLL,我用gendef和dlltool创建。一个库链接。
I am using gfortran v.4.6 on windows, linking to a library to which I have no source code. The library came to me as a DLL and I used gendef and dlltool to create an .a library to link to.
在code如下:
PROGRAM cmod
USE, INTRINSIC :: ISO_C_BINDING
INTERFACE
LOGICAL (C_BOOL) FUNCTION clover(scen,reg,soil,top,rain,depth,numd,nums,numb, numd,addn,srate,stype,nloss,ploss,ErrStr) BIND (C, name = "ClOvr")
USE, INTRINSIC :: ISO_C_BINDING
INTEGER (C_INT), INTENT(IN), VALUE :: scen,reg,soil,topo,depth
REAL (C_DOUBLE), INTENT(IN), VALUE :: rain,numd,nums,numb,numd
REAL (C_DOUBLE), INTENT(IN), VALUE :: addn,srate
INTEGER (C_INT), INTENT(IN), VALUE :: stype
REAL (C_DOUBLE), INTENT(OUT) :: nloss,ploss
CHARACTER(C_CHAR), INTENT(OUT) :: ErrStr(*)
END FUNCTION clover
END INTERFACE
.....
do
.....
result = clover(a,b,c, d, e, f, g, h, sb, sd, an, sr, st,lossx,lossy,err)
......
result = clover(a,b,c, d, e, f, g, h, sb, sd, an, sr, st,lossx,lossy,err)
end do
END PROGRAM cmod
我没有C code,但我从IBM的Fortran移植。
I don't have the C code, but I am porting from IBM Fortran.
该接口的声明包含这两个行
The interface declaration contains these two lines
!DEC$ ATTRIBUTES VALUE :: scen,reg,soil,top,rain,depth,numd,nums,numb,numd,addn,srate,stype
!DEC$ ATTRIBUTES REFERENCE :: lossx,lossy,ErrStr
IBM的FORTRAN使用以下code加载库并访问功能:
The IBM fortran uses the following code to load the library and access the function:
pointer (q,clover)
p = loadlibrary("clover.dll")
q = getprocaddress(p, "ClOvr")
所以我可能会丢失在翻译的东西GNU FORTRAN
So I might be missing something in the translation to GNU fortran
推荐答案
我发现我的问题:当我编译,我必须使用 -mrtd(sdtcall)
I've found my problem: when I compile, I have to use the -mrtd (sdtcall)
这篇关于重复调用的Fortran从C函数时分段故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!