是否可以从C ++调用Fortran接口

编程入门 行业动态 更新时间:2024-10-28 21:27:15
本文介绍了是否可以从C ++调用Fortran接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下无法编译的代码.如我在下面尝试的那样,是否可以将Fortran接口作为C ++中的重载函数调用?

这是Fortran代码:

module functions use, intrinsic :: iso_c_binding, only : c_double, c_int implicit none interface increment bind(c, name="increment") module procedure increment_int, increment_double end interface contains subroutine increment_int(a, b) integer(c_int), intent(inout) :: a integer(c_int), value :: b a = a + b end subroutine subroutine increment_double(a, b) real(c_double), intent(inout) :: a real(c_double), value :: b a = a + b end subroutine end module functions

这是C ++代码:

#include <iostream> namespace { extern "C" void increment(int&, int); extern "C" void increment(double&, double); } int main() { int a = 6; const int b = 2; double c = 6.; const int d = 2.; increment(a, b); increment(c, d); std::cout << "a = " << a << std::endl; std::cout << "c = " << c << std::endl; return 0; }

解决方案

否,这是不可避免的.但是您可以实例化一个模板来调用两者.或者只是对这两个外部C函数进行C ++通用包装.您绝对不能让Fortran导出接口.接口只是一种描述,说明如何在Fortran内部以某些名称调用这两个子例程.

此问题将接口模块过程从fortran导入到C 非常相似.我什至最初本来是关闭您的副本,但是后来我改变了主意,因为尝试的解决方案略有不同.

但是原理是一样的. Fortran泛型和C ++泛型不兼容.而且您之间有C,没有相似类型的泛型.

注意:正如@RichardCritten所建议的那样,您也不应在extern C函数中通过引用传递.编译器可能会对其进行编译,并通过按值传递指针来实现,但不能保证.只需传递一个指针即可.有关更多信息,请参见 C ++按引用参数和C链接. >

I have the following code that does not compile. Is it possible to call the Fortran interface as overloaded functions in C++, as I try below?

This is the Fortran code:

module functions use, intrinsic :: iso_c_binding, only : c_double, c_int implicit none interface increment bind(c, name="increment") module procedure increment_int, increment_double end interface contains subroutine increment_int(a, b) integer(c_int), intent(inout) :: a integer(c_int), value :: b a = a + b end subroutine subroutine increment_double(a, b) real(c_double), intent(inout) :: a real(c_double), value :: b a = a + b end subroutine end module functions

And this is the C++ code:

#include <iostream> namespace { extern "C" void increment(int&, int); extern "C" void increment(double&, double); } int main() { int a = 6; const int b = 2; double c = 6.; const int d = 2.; increment(a, b); increment(c, d); std::cout << "a = " << a << std::endl; std::cout << "c = " << c << std::endl; return 0; }

解决方案

No, it cannot be avoided. But you can instantiate a template to call the two. Or just make a C++ generic wrapper to those two extern C functions. You definitely cannot make Fortran to export the interface. Interface is just a description how to call the two subroutines under some name internally in Fortran.

This question importing interface module procedure from fortran into C is very similar. I even originally closed yours as a duplicate, but then I changed my mind because the attempted solution is slightly different.

But the principle is the same. Fortran generics and C++ generics are not compatible. And you have C between them which has no generics of similar type.

Note: As @RichardCritten suggest you also should not pass by reference in an extern C function. The compiler probably compiles it and implements as passing a pointer by value, but it is not guaranteed. Just pass a pointer. See C++ by-reference argument and C linkage for more.

更多推荐

是否可以从C ++调用Fortran接口

本文发布于:2023-06-04 19:05:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/503290.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   Fortran

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!