通过调用Fortran中的C ++函数将Fortran int数组传递给C ++ [英] Passing a Fortran int array to C++ by calling C++ function in Fortran

查看:283
本文介绍了通过调用Fortran中的C ++函数将Fortran int数组传递给C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Fortran子程序中调用C ++函数。这个C ++函数应该更新一个整数数组。这里是我写的一个非工作代码。问题是什么?

 ! Fortran函数调用C ++函数。 

子例程my_function()

整数(4)ar(*)

整数(4)get_filled_ar

!此处需要正确的语法。
ar = get_filled_ar()
end


// C ++函数:

externC{
void get_filled_ar (int * ar){
ar [0] = 1;
ar [1] = 10;
ar [3] = 100;
}
}


解决方案

Fortran 2003有一个标准,从而平台和编译器独立的方式调用C从Fortran,以及任何使用C调用接口的语言。同样从C调用Fortran。虽然各种编译器编写器逐渐增加Fortran 2003功能,并且几乎没有完整的2003编译器,ISO C Binding已经在许多编译器中可用了一段时间。 ISO C Binding比以前的临时技术更好,这些技术有时记录不良,并且在编译器和平台之间有所不同。要从Fortran调用C,你写一个接口告诉Fortran编译器它应该使用C调用约定和C类型。



这是一个例子。正如Mike写的,由于C ++函数返回void,在Fortran中将其作为子例程并调用它。因此,它不需要给一个类型。另外,在Fortran的某个地方,你必须为数组预留存储空间 - 最简单的方法是使用带有数值的声明。

 程序test_call_C 

使用iso_c_binding

implicit none

interface c_interface

子程序get_filled_ar(ar)bind(C,name =get_filled_ar)

使用iso_c_binding

implicit none

integer(c_int),intent(out),dimension(*):: ar

end子程序get_filled_ar

end interface c_interface


整数(c_int),维度(0:3):: ar

调用get_filled_ar(ar)
b $ b write(*,*)Fortran:ar:,ar

stop

结束程序test_call_C
pre>

和C:

  void get_filled_ar b int ar [] 
){

ar [0] = 1;
ar [1] = 10;
ar [2] = 100;

return;

}

示例命令:

  gcc -c get_filled_ar.c 
gfortran get_filled_ar.o test_call_C.f90 -o test_call_C.exe
./test_call_C.exe

要调用您的C ++代码,请使用以下命令。

  g ++ -c cplusplus在绑定中指定的名称不需要尾随下划线.cc 
gfortran cplusplus.o test_call_C.f90 -o test_call_Cplusplus.exe
./test_call_Cplusplus.exe


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;
        }
    }

解决方案

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.

program test_call_C

use iso_c_binding

implicit none

interface c_interface

   subroutine get_filled_ar (ar) bind (C, name = "get_filled_ar")

   use iso_c_binding

   implicit none

   integer (c_int), intent (out), dimension (*) :: ar

   end subroutine get_filled_ar

end interface c_interface


integer (c_int), dimension (0:3) :: ar

call get_filled_ar (ar)

write (*, *) "Fortran: ar:", ar

stop

end program test_call_C

and in C:

void get_filled_ar (
   int ar []
) {

   ar [0] = 1;
   ar [1] = 10;
   ar [2] = 100;

   return;

}

Example commands:

gcc -c get_filled_ar.c
gfortran get_filled_ar.o test_call_C.f90  -o test_call_C.exe
./test_call_C.exe

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.

g++ -c cplusplus.cc
gfortran cplusplus.o test_call_C.f90  -o test_call_Cplusplus.exe
./test_call_Cplusplus.exe

这篇关于通过调用Fortran中的C ++函数将Fortran int数组传递给C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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