winston2017 发表于 2017-4-25 00:45:43

fortran调用c生成的dll出现问题

fortran在调用一个开源软件的dll时,发生错误,请大家指教。
fortran代码如下:
module cprog
interface
integer function SWMMSTEP(elpTime)                     
!DEC$ ATTRIBUTES C, ALIAS:'swmm_step'::SWMMSTEP
!DEC$ ATTRIBUTES REFERENCE::elpTime
real(kind=8)::elpTime
end function
end interface
real(kind=8)::elpTime       !全局变量
end module cprog

program F90useVCdll
use cprog
implicit none
integer::errorNum
do                  !do_01
      errorNum=SWMMSTEP(elpTime)
       if ((elpTime <= 0.0).or.(errorNum .ne. 0)) exit
enddo
end program
SWMMSTEP的c语言代码如下(DateTime就是double)int DLLEXPORT swmm_step(DateTime* elapsedTime)
{
    ...(代码太长)
            *elapsedTime = NewRoutingTime / MSECperDAY;
    return ErrorCode;
}
程序一开始可以运行,运行到一段时间就会出现以下错误,现在检查到主要是SWMMSTEP在do循环中发生问题,请大家指教。

fcode 发表于 2017-4-25 07:58:02

看起来似乎没有问题(在混编接口部分)。断点单步调试一下吧,也许是开源软件的事儿。

此外,你这种混编方法太老土了。有更新潮(主要是规范)的混编方式:

module cprog
interface
    integer function SWMMSTEP(elpTime) Bind(C,Name="swmm_step")
      use , intrinsic :: ISO_C_Binding
      real(kind=C_DOUBLE)::elpTime
    end function
end interface
real(kind=8)::elpTime       !全局变量
end module cprog

program F90useVCdll
use cprog
implicit none
integer::errorNum
do      
    errorNum=SWMMSTEP(elpTime)
    if ((elpTime <= 0.0).or.(errorNum .ne. 0)) exit
enddo
end program F90useVCdll

winston2017 发表于 2017-4-25 08:14:28

fcode 发表于 2017-4-25 07:58
看起来似乎没有问题(在混编接口部分)。断点单步调试一下吧,也许是开源软件的事儿。

此外,你这种混编方 ...

您好,试了您说的新方法,程序直接提示错误了

pasuka 发表于 2017-4-25 08:48:31

1、通过ISO C BINDING可以让C访问Fortran的全局变量,虽然不推荐,参考:
Interoperable Global Variables - The GNU Fortran Compiler
https://gcc.gnu.org/onlinedocs/gcc-6.3.0/gfortran/Interoperable-Global-Variables.html#Interoperable-Global-Variables
2、elpTime变量为啥没有赋初值?

winston2017 发表于 2017-4-25 09:03:01

pasuka 发表于 2017-4-25 08:48
1、通过ISO C BINDING可以让C访问Fortran的全局变量,虽然不推荐,参考:
Interoperable Global Variables...

1、在module那里定义的elpTime不是全局变量吗?为什么还需要按照这个来定义?
2、不好意思误导您了,贴出来的是我代码的一部分,在原代码中前面有赋值的。
3、非常感谢您的回答!

winston2017 发表于 2017-4-25 09:36:46

很奇怪,现在有时候调试提示错误又变成了访问冲突



pasuka 发表于 2017-4-25 10:26:09

winston2017 发表于 2017-4-25 09:03
1、在module那里定义的elpTime不是全局变量吗?为什么还需要按照这个来定义?
2、不好意思误导您了,贴出 ...

不加额外申明,C程序哪里认得elpTime这个全局变量
页: [1]
查看完整版本: fortran调用c生成的dll出现问题