简体   繁体   English

反复从Fortran调用C函数时出现分段错误

[英]Segmentation fault when calling a C function from Fortran repeatedly

I have a Fortran 90 program which repeatedly calls on a C function. 我有一个Fortran 90程序,该程序反复调用C函数。 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: 第一次使用该函数时一切正常,但是第二次代码尝试访问该函数时,出现以下错误:

Program received signal SIGSEGV: Segmentation fault - invalid memory reference. 程序收到信号SIGSEGV:分段错误-无效的存储器引用。

I am using gfortran v.4.6 on windows, linking to a library to which I have no source code. 我在Windows上使用gfortran v.4.6,链接到我没有源代码的库。 The library came to me as a DLL and I used gendef and dlltool to create an .a library to link to. 该库以DLL的形式出现,我使用gendef和dlltool创建了一个链接到的.a库。

The code looks like: 代码如下:

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

I don't have the C code, but I am porting from IBM Fortran. 我没有C代码,但是我正在从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

The IBM fortran uses the following code to load the library and access the function: IBM fortran使用以下代码来加载库并访问该函数:

pointer (q,clover)
p = loadlibrary("clover.dll")
q = getprocaddress(p, "ClOvr")

So I might be missing something in the translation to GNU fortran 所以我可能在GNU fortran的翻译中遗漏了一些东西

我发现了我的问题:编译时,我必须使用-mrtd (sdtcall)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM