从 C 调用 Fortran 77

发布于 2024-09-24 07:50:11 字数 417 浏览 8 评论 0原文

我在 fortran 77 中有一个 ema 函数。我如何从 C 调用 fortran 函数。我知道在 fortran 2003 中有一个 iso_c_bindings 但我需要一些针对旧 77 标准的解决方案。

subroutine ema(inarray,leninarray,n,outarray,lenoutarray)

integer leninarray, lenoutarray, n, i
double precision inarray(leninarray), outarray(lenoutarray)

do 10 i = n+1, leninarray
    outarray = inarray(i) * (1/n) + outarray(i-1) * (1-(1/n))

continue
end

感谢您的帮助 问候

I have an ema function in fortran 77. How can I call to fortran function from C. I know that in fortran 2003 there is a iso_c_bindings but I need some solution for old 77 standard.

subroutine ema(inarray,leninarray,n,outarray,lenoutarray)

integer leninarray, lenoutarray, n, i
double precision inarray(leninarray), outarray(lenoutarray)

do 10 i = n+1, leninarray
    outarray = inarray(i) * (1/n) + outarray(i-1) * (1-(1/n))

continue
end

Thanks for any help
regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲喜皆因你 2024-10-01 07:50:11

Fortran 的调用约定与 C 几乎相同,但有以下差异:

  • 一切都通过引用传递:您需要将地址传递给 fortran 例程
  • 参数的顺序是相同的(从从右到左)
  • 调用 fortran 函数(不是 fortran 子例程)时,需要将返回值的地址压入堆栈中的参数后面。在相应的 C 原型中,这相当于将函数声明为 void,并具有一个额外的 first 参数,该参数是指向返回值的指针。
  • 有时,您需要在 Fortran 名称后附加一个额外的下划线。这需要一些实验。

例如,如果您有一个 Fortran 例程,

subroutine ema(inarray,leninarray,n,outarray,lenoutarray)

它会变成

void ema(double*, int*, int*, double*, int*)

void ema_(double*, int*, int*, double*, int*)

取决于您的机器。在 C++ 中,

extern "C" void ema(double*, int*, int*, double*, int*)

您希望或不要忘记 fortran 数组从 1 开始。

extern "C" void ema_(double*, int*, int*, double*, int*)

当您将索引传递给例程时,

Fortran has pretty much the same calling convention as C, but with the following differences:

  • Everything is passed by reference: you need to pass addresses to the fortran routine
  • The order of the arguments are the same (pushed from right to left)
  • The address of a return value needs to be pushed on the stack after the arguments when calling a fortran function (not fortran subroutines). In the corresponding C prototype, this amounts to declaring the function void, and having an extra first argument which is a pointer to the return value.
  • Sometimes, you need to append an extra underscore to the fortran name. This needs some experimentation.

E.g. if you have a fortran routine

subroutine ema(inarray,leninarray,n,outarray,lenoutarray)

it turns into

void ema(double*, int*, int*, double*, int*)

or

void ema_(double*, int*, int*, double*, int*)

depending on your machine. In C++ you want

extern "C" void ema(double*, int*, int*, double*, int*)

or

extern "C" void ema_(double*, int*, int*, double*, int*)

Don't forget that fortran arrays start as 1 when you pass indices to the routines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文