Fortran 如何返回数组?
Fortran 库 CUBPACK 中的子例程 Rule_Tn
需要一个描述积分向量函数的参数 Integrand
。因为
INTERFACE
FUNCTION Integrand(NF,X) RESULT(Value)
USE Precision_Model
INTEGER, INTENT(IN) :: NF
REAL(KIND=STND), DIMENSION(:), INTENT(IN) :: X
REAL(KIND=STND), DIMENSION(NF) :: Value
END FUNCTION Integrand
END INTERFACE
我想从 C 代码调用 Rule_Tn
,所以我需要在 C 中定义一个与上面的接口完全匹配的函数类型。因此我尝试弄清楚 Fortran 函数如何返回数组。起初我认为以下 C 签名
void Integrand(double* value, const int* nf, const int* x);
与上面的接口匹配。但大错特错了!我遇到了段错误。我已经测试过 double 是 REAL(KIND=STND) 对应的类型,STND
来自模块 Precision_Model
。
现在谁能告诉我什么是正确的签名?我正在使用 GNU Fortran 和 C 编译器。
The subroutine Rule_Tn
in the Fortran library CUBPACK needs a parameter Integrand
describing the integrated vector function. It's a
INTERFACE
FUNCTION Integrand(NF,X) RESULT(Value)
USE Precision_Model
INTEGER, INTENT(IN) :: NF
REAL(KIND=STND), DIMENSION(:), INTENT(IN) :: X
REAL(KIND=STND), DIMENSION(NF) :: Value
END FUNCTION Integrand
END INTERFACE
Since I want to call Rule_Tn
from C code I need to define a function type in C exactly matching to this interface above. Thus I tried figure out how a Fortran function returns arrays. At first I supposed the following C signature
void Integrand(double* value, const int* nf, const int* x);
matches to the interface above. But far wrong! I got a segfault. And I already tested that double is the corresponding type to REAL(KIND=STND)
, the STND
comes out of the module Precision_Model
.
Now can anyone tell me what's the right signature? I'm using the GNU Fortran and C compilers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅 GNU 文档。看来您在 Fortran 和 C 之间以不同的顺序提供了参数。尝试将
value
最后放在 C 原型中。另外,您在
FUNCTION
行上缺少bind(C)
。See GNU docs. It looks like you provided the arguments in a different order between Fortran and C. Try putting
value
last in the C prototype.Also, you are missing
bind(C)
on theFUNCTION
line.