从 C 调用 FORTRAN 子例程
我正在尝试从 C 调用 FORTRAN 函数
我的问题是:
如果
fortRoutine
是我的 Fortran 子例程的名称,那么我将从 C 调用它 作为fortRoutine_
。如果fortRoutine
仅包含一个字符数组参数,那么我可以这样传递:fortRoutine_("我使用的是 fortran");
当调用 FORTRAN 子例程时,何时应该使用按值传递以及何时按引用传递?
由于我是C语言新手,所以我对此一无所知。如果可能的话,请推荐一些好的教程链接。
I am trying to call a FORTRAN function from C
My questions are:
If
fortRoutine
is the name of my fortran subroutine, then I am calling this from C
asfortRoutine_
. IffortRoutine
contains only one character array argument, then can I pass like this:fortRoutine_("I am in fortran");
While calling FORTRAN subroutines, when should I use pass by value and when pass by reference?
As I am new to C, I do not have a clue about this. If possible, please suggest some good tutorial links as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在执行此操作的方法是在 Fortran 端使用 Fortran ISO C 绑定。这是 Fortran 2003 语言标准的一部分,可在许多编译器中使用;它不是特定于 gcc 的。本网站的许多答案中都对此进行了描述。作为语言标准的一部分,它独立于编译器和平台。并且您不需要了解编译器的内部传递约定。 ISO C 绑定在 Fortran 子例程或函数的声明中使用时,会导致 Fortran 编译器使用 C 调用约定,以便可以直接从 C 调用该过程。您不需要添加隐藏参数或名称修改Fortran 子程序名称,即没有下划线。链接器使用的名称来自“bind”选项。
字符串是一个困难的情况,因为从技术上讲,在 C 中它们是字符数组,并且您必须在 Fortran 中匹配它。您还必须处理字符串的不同定义:C 以 null 结尾,Fortran 固定长度并用空格填充。该示例展示了这是如何工作的。数字更容易。数组的唯一问题是 C 是行优先而 Fortran 是列优先,因此多维数组会被转置。
将 C 编译为目标文件,然后
使用 gfortran 编译 fortran 并链接两者:
输出为:
The way to do this now is to use the Fortran ISO C Binding on the Fortran side. This is part of the Fortran 2003 language standard and is available in many compilers; it is not specific to gcc. It has been described in many answers on this site. As part of the language standard, it is compiler and platform independent. And you do not need to know about the internal passing conventions of the compiler. The ISO C Binding, when used in the declaration of a Fortran subroutine or function, causes the Fortran compiler to use the C calling conventions so that that procedure can be directly called from C. You do not need to add hidden arguments or name mangle the Fortran subroutine name, i.e., no underscores. The name used by the linker comes from the "bind" option.
Strings are a difficult case because technically in C they are arrays of characters and you have to match this in the Fortran. You also have to deal with the different definitions of strings: C is null terminated, Fortran fixed length and padded with blanks. The example shows how this works. Numbers are easier. The only issue with arrays is that C is row-major and Fortran column-major so that multi-dimensional arrays are transposed.
and
You compile the C to an object file and use gfortran to compile the fortran and link both:
Output is:
当然,这一切都取决于您的 FORTRAN 编译器,但一般来说:
不,您需要为字符串传递一个隐藏的长度参数。一些编译器将这些参数与其他参数直接交错放置在字符串之后。其他人,将所有字符串长度参数分组到参数列表的末尾。
几乎总是通过引用传递,但您可以使用 FORTRAN 端虚拟参数上的属性来切换此行为。
以下是一些参考资料,适用于各种编译器:
Of course this all depends on your FORTRAN Compiler, but generally speaking:
No, you'll need to pass a hidden length argument for your string. Some compilers interleave these with the other parameters, directly after the string. Others, group all string length arguments at the end of the argument list.
Almost always it is pass by reference, but you can toggle this behavior using attributes on the dummy arguments on the FORTRAN side.
Here are some references, which are applicable based on various compilers:
答案取决于编译器和系统(从技术上讲,它的 ABI)。对于 GCC(C、C++、Ada 和 Fortran 编译器),请阅读 Fortran 混合编程章节。
The answer depends upon the compiler and the system (technically, its ABI). For GCC (which is a C, a C++, an Ada, and a Fortran compiler) read the fortran mixed programming chapter.