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

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

问题描述

我如何在使用可选参数的C ++头文件中引用Fortran函数?我会在头文件中为每个可能的呼叫组合添加一个原型吗?或者,这甚至有可能?



例如,Fortran:

 子程序foo(a,b,c)绑定(c)
真实,意图(in),可选项:: a,b,c
...
结束子程序foo


解决方案

除非您使子程序 bind(C)



一旦你使它成为 bind(C),它只是传递了一个指针,它可以在C端为NULL。

 子程序foo(a,b ,c)bind(C,name =foo)
real,intent(in),optional :: a,b,c
...
end子程序foo

(为了更大的可移植性 real(c_float)来自 iso_c_binding 模块应该被使用,但是这与这个问题有点相似)



在C(++)中

  externC{
void foo(float * a,float * b,float * c);
}

foo(& local_a,NULL,NULL);

然后您可以创建一个调用 foo

Fortran允许Fortran在技术规范ISO / IEC TS 29113:2012中进一步实现Fortran与C的互操作性。


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?

For instance, Fortran:

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

解决方案

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

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")
   real, intent(in), optional :: a, b, c
   ...
end subroutine foo

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

In C(++)

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

foo(&local_a, NULL, NULL);

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

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

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

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