Fortran接口以调用返回指向数组的指针的C函数 [英] Fortran interface to call a C function that returns a pointer to an array

查看:138
本文介绍了Fortran接口以调用返回指向数组的指针的C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量搜索,我发现我认为最接近问题的答案是在

After much searching, I found what I believe to be the closest answer to my problem is on Stack Overflow (SO) at Fortran interface to call a C function that return a pointer, (posted nearly 10 years ago!)

之所以这样引用,是因为使用该示例可使代码保持简单,并且仍然说明了我的问题.

I quote this because using that example keeps the code simple and still illustrates my problem.

我想返回一个已经在C ++中创建/分配的内存的数组,并且能够在Fortran中分析答案,因为这是此应用程序的大部分代码所在的地方.我的应用程序进入C ++生成整数数组答案,并通过C接口将其返回给Fortran程序.原始的SO示例使用单个双精度变量作为返回值.我将其更改为整数,因为这就是我在应用程序中要处理的内容.示例代码(已更改)有效.

I want to return an array that has been created/memory allocated in C++ and be able to analyse the answer in Fortran, because that is where the bulk of the code for this application lies. My application goes off into C++ to produce the integer array answer and returns it to the Fortran program via the C interface. The original SO example used a single double precision variable as the return. I’ve changed it to integer because that is what I will be dealing with in my application. The example code (as changed) works.

我已在注释中突出显示了为返回数组指针而试图进行的更改,但我的想法已经用完了.(我可以说,哦,在糟糕的过去,我可以将整数等同于iarray(1)并超出数组的大小."但我不会.拥有编码保护是很好的,但是有时真令人沮丧.)

I have highlighted with comments the changes that I have tried to make to return an array pointer, but I’ve run out of ideas. (I could say, "Oh for the bad old days when I could just equivalence an integer to an iarray(1) and go beyond the size of the array", but I won’t. It’s good to have coding protections, but sometimes it gets frustrating.)

我正在使用Visual Studio 2017和Intel Fortran parallel_studio_xe_2019_update5_composer.

I am using Visual Studio 2017 and Intel Fortran parallel_studio_xe_2019_update5_composer.

我修改的原始SO代码示例:

My modified example of the original SO code:

! ps_test_pointers.f90

program foo
  use, intrinsic :: iso_c_binding, only : c_ptr,        &
                                          c_f_pointer,  &
                                          c_int
  implicit none
  type(c_ptr) :: c_p!(:) ! <-------
  integer(c_int), pointer :: f_p!(:) ! <-------

  interface
    function foofunc() bind(c)
      import :: c_ptr
      implicit none  
      type(c_ptr) :: foofunc!(:) ! <-------
    end function foofunc
  end interface

  c_p = foofunc()
  call c_f_pointer(c_p, f_p)
  print *, f_p

end program foo

// ps_test_pointersC.cpp : 'Subroutine' only.

extern "C" {

    int bar[3] = { 2, 3, 4 };
    
    int *foofunc() {
        return bar;
    }

}

如上所述,该代码有效,因为它可以打印出数组的第一个元素("2").

As I said above, the code works, in the sense that it prints out the first element of the array (‘2’).

如果我在f_p的定义中添加'(:)',则代码将正确编译,但是当我运行它时,程序将由于运行时错误而失败:"forrtl:严重(408):要塞:(7):在调用c_f_pointer(c_p,f_p)"行尝试使用未与目标关联的指针F_P.

If I add the ‘(:)’ to the definition of f_p, the code compiles without error, but when I run it, the program fails with the run-time error: "forrtl: severe (408): fort: (7): Attempt to use pointer F_P when it is not associated with a target" at the line "call c_f_pointer(c_p, f_p)".

我尝试将c_p声明为数组("c_p(:)"),但在同一位置却遇到相同的错误.

I have tried declaring c_p as an array ("c_p(:)"), but I get the same error in the same place.

我还尝试将c_p用作子例程的参数–仍然仅使用整数:

I have also tried calling c_p as an argument to a subroutine – still only using integers:

    ! ps_test_pointers.f90
    
    program foo
      use, intrinsic :: iso_c_binding, only : c_ptr,        &
                                              c_f_pointer,  &
                                              c_int
      implicit none
      type(c_ptr) :: c_p!(:) ! <-------
      integer(c_int), pointer :: f_p!(:) ! <-------
    
      interface
        subroutine foofunc(c_p) bind(c)
          import :: c_ptr
          implicit none  
          type(c_ptr) :: c_p!(:) ! <-------
        end subroutine foofunc
      end interface
    
      call foofunc(c_p)
      call c_f_pointer(c_p, f_p)
      print *, f_p
    
    end program foo
    

    // ps_test_pointersC.cpp : 'Subroutine' only.
    
    extern "C" {
    
        int bar[3] = { 2, 3, 4 };
        
        void foofunc(int *rtn) {
            rtn = bar;
        }
    
    }

但是在C函数中创建的指针永远不会在返回时分配给c_p(因此从未定义f_p).

but the created pointer in the C function never gets assigned to c_p on return (hence f_p is never defined).

仔细阅读问题,希望我不会在编译器实现的前沿,并且在限制加紧但不能应对所有用例之间暴露出一个问题!

Reading around the problem, I hope I’m not at the bleeding edge of compiler implementation and have exposed a problem between restrictions tightening but not coping with all the use cases!

对此有解决方案吗?

推荐答案

RE是子例程方法,我认为我们可能需要将 c_p 声明为 int ** (而不是而不是C/C ++端的 int * )来通过参数关联(而不是函数返回值)获取 bar 的地址.像...

RE the subroutine approach, I think we probably need to declare c_p as int** (rather than int*) on the C/C++ side to get the address of bar via argument association (rather than function return value). So something like...

main.f90:

program foo
  use, intrinsic :: iso_c_binding, only : c_ptr,        &
                                          c_f_pointer,  &
                                          c_int
  implicit none
  type(c_ptr) :: c_p
  integer(c_int), pointer :: f_p(:)
  integer(c_int) :: nsize

  interface
    subroutine foosub( c_p, nsize ) bind(c)
      import :: c_ptr, c_int
      implicit none  
      type(c_ptr)    :: c_p    !<-- sends the pointer to c_p
      integer(c_int) :: nsize  !<-- sends the pointer to nsize
    end subroutine
  end interface

  call foosub( c_p, nsize )
  call c_f_pointer( c_p, f_p, [nsize] )

  print *, "nsize  = ", nsize
  print *, "f_p(:) = ", f_p(:)

end program

sub.cpp:

extern "C" {
    int bar[3] = { 2, 3, 4 };
    
    void foosub( int** rtn, int* nsize ) {
        *rtn = bar;
        *nsize = sizeof(bar) / sizeof(int);
    }
 }

编译&运行:

$ g++-10 -c sub.cpp
$ gfortran-10 -c main.f90
$ g++-10 main.o sub.o -lgfortran
$ ./a.out
 nsize  =            3
 f_p(:) =            2           3           4

这篇关于Fortran接口以调用返回指向数组的指针的C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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