将 Fortran int 数组传递给 C++通过调用 C++ Fortran 语言中的函数
我正在尝试在 Fortran 子例程中调用 C++ 函数。这个 C++ 函数应该更新一个整数数组。这是我写的一段不起作用的代码。有什么问题吗?
! Fortran function that calls a C++ function.
subroutine my_function()
integer(4) ar(*)
integer(4) get_filled_ar
! Need correct syntax here.
ar = get_filled_ar()
end
// C++ function:
extern "C" {
void get_filled_ar(int *ar){
ar[0] = 1;
ar[1] = 10;
ar[3] = 100;
}
}
I am trying to call a C++ function in a Fortran subroutine. This C++ function is supposed to update an integer array. Here is a non-working code I wrote. What is the issue?
! Fortran function that calls a C++ function.
subroutine my_function()
integer(4) ar(*)
integer(4) get_filled_ar
! Need correct syntax here.
ar = get_filled_ar()
end
// C++ function:
extern "C" {
void get_filled_ar(int *ar){
ar[0] = 1;
ar[1] = 10;
ar[3] = 100;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Fortran 2003 提供了一种标准的、独立于平台和编译器的方法来从 Fortran 调用 C,以及使用 C 调用接口的任何语言。还要从 C 调用 Fortran。虽然各个编译器编写者正在逐渐添加 Fortran 2003 功能,并且几乎没有完整的 2003 编译器,但 ISO C 绑定在许多编译器中已经可用了一段时间。 ISO C 绑定比以前的临时技术工作得更好,这些技术有时记录很少,并且在编译器和平台之间存在差异。要从 Fortran 调用 C,您需要编写一个“接口”来告诉 Fortran 编译器它应该使用 C 调用约定和 C 类型。
这是一个例子。正如 Mike 所写,由于 C++ 函数返回 void,因此在 Fortran 中将其视为子例程并调用它。因此不需要给它指定类型。另外,在 Fortran 中的某个地方,您必须为数组保留存储空间——最简单的方法是使用带有维度数值的声明。并且您需要某种语言的主程序。
在 C 中:
示例命令:
要调用 C++ 代码,请使用以下命令。 “bind”中指定的名称不需要尾随下划线,因此您的 C++ 代码可以直接运行。
With Fortran 2003 there is a standard and thus platform and compiler independent way to call C from Fortran, and also any language that uses the C calling interface. Also to call Fortran from C. While the various compiler writers are gradually adding Fortran 2003 features and there are few complete 2003 compilers, the ISO C Binding has been available for some time in many compilers. The ISO C Binding works better than the previous ad hoc techniques, which were sometimes poorly documented, and varied between compilers and platforms. To call C from Fortran, you write an "interface" that tells the Fortran compiler that it should use the C calling conventions, and C types.
Here is an example. As Mike wrote, since the C++ function returns void, treat it in the Fortran as a subroutine and call it. Thus it doesn't need to be given a type. Also, somewhere in the Fortran you have to reserve storage for the array -- the easiest way is with a declaration with numeric value for the dimension. And you need a main program in some language.
and in C:
Example commands:
To call your C++ code, use the following commands. The name specified in the "bind" obviates the need for a trailing underscore so your C++ code works directly.
这是一个完整的程序,其中包含您的代码,应该可以工作(至少在使用
gcc
/gfortran
的 linux 上)。在 fortran 文件
fortran.f
中,输入:在
cplusplus.cc
中输入:然后构建(在 linux 上使用
gcc
/gfortran< /code>,在其他系统上可能有所不同):
Here's a full program with your code in it that should work (at least on linux with
gcc
/gfortran
).In the fortran file
fortran.f
, put:In
cplusplus.cc
put:Then build (on linux with
gcc
/gfortran
, might be different on other systems) with: