Fortran 接口调用返回指向数组的指针的 C 函数 [英] Fortran interface to call a C function that returns a pointer to an array
问题描述
经过多次搜索,我发现我认为最接近我的问题的答案是Fortran 接口调用返回指针的 C 函数,(近 10 年前发布!)
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:severe (408): fort:(7):在call 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!
有没有办法解决这个问题?
Is there a solution to this?
推荐答案
RE 子程序方法,我想我们可能需要将 c_p
声明为 int**
(而不是而不是 int*
) 在 C/C++ 端通过参数关联(而不是函数返回值)获取 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屋!