Calling Fortran subroutines with optional arguments from C++(从 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 子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 C++ 调用带有可选参数的 Fortran 子例程
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
