从 C++ 调用带有可选参数的 Fortran 子例程 [英] Calling Fortran subroutines with optional arguments from C++

查看:25
本文介绍了从 C++ 调用带有可选参数的 Fortran 子例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用可选参数的 C++ 标头中引用 Fortran 函数?对于每个可能的调用组合,我会在标题中放置一个原型吗?或者这甚至可能吗?

How would I reference a Fortran function in a C++ header that uses optional arguments? Would I have a prototype in the header for each possible combination of calls? Or is this even possible?

例如,Fortran:

For instance, Fortran:

subroutine foo(a, b, c) bind(c)
   real, intent(in), optional :: a, b, c
   ...
end subroutine foo

推荐答案

这是不可能的,至少是可移植的,除非你制作子程序 bind(C).

It is not possible, at least portably, unless you make the subroutine bind(C).

一旦你做了 bind(C),它只是传递一个在 C 端可以为 NULL 的指针.

Once you make it bind(C), it is just passing of a pointer which can be NULL on the C side.

subroutine foo(a, b, c) bind(C, name="foo")
   use iso_c_binding, only: c_float
   real(c_float), intent(in), optional :: a, b, c
   ...
end subroutine foo

(为了更好的可移植性,我使用了 iso_c_binding 模块中的 real(c_float) ,但这与这个问题有些相干)

(for greater portability I used real(c_float) from the iso_c_binding module, but that is somewhat tangential to this question)

在 C(++) 中

extern "C"{
  void foo(float *a, float *b, float *c);
}

foo(&local_a, NULL, NULL);

然后您可以创建一个调用 foo 并使用 C++ 样式可选参数的 C++ 函数.

and then you can make a C++ function which calls foo and which employs C++-style optional parameters.

Fortran 技术规范 ISO/IEC TS 29113:2012 中允许此功能用于 Fortran 与 C 的进一步互操作性,后来被合并到 Fortran 2018 中.

This capability was allowed in Fortran in Technical Specification ISO/IEC TS 29113:2012 on further interoperability of Fortran with C and was later incorporated into Fortran 2018.

这篇关于从 C++ 调用带有可选参数的 Fortran 子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆