我有一个C代码(我不是上述代码的所有者),它生成了以下 struct :
struct Vector
{
int size;
double data[3];
};
struct VerticesVect
{
int size;
struct Vector* data;
};
And so on:有许多额外的 struct (通常是先前定义的 struct ).
我需要调用所述C代码/库从一个Fortran代码.Fortran代码将使用以前C生成的 struct .Fortran代码不会以任何方式编写/编辑它们,它只需要它们作为输入.
我试图找到一种方法来声明这些类型在Fortran中,以便我可以使用它们.
use, intrinsic :: ISO_C_BINDING
其次是
type, bind(c) :: Vector
integer(kind=C_INT32_T) :: size
real(kind=C_DOUBLE) :: data(3)
end type Vector
显然没问题
type, bind(c) :: VerticesVect
integer(kind=c_int) :: size
type(Vector), dimension(:) :: data
end type VerticesVect
但无效("数据必须具有明确的形状"),也
type, bind(c) :: VerticesVect
integer(kind=c_int) :: size
type(Vector), pointer :: data
end type VerticesVect
("组件'data'不能具有POINTER属性,因为它是BIND(C)派生类型'vertesvect'的成员")
我所能想到的可分配、指针、定位(:)的任何组合似乎也不有效.
不幸的是,指定一个静态尺寸,使其足够大以涵盖所有情况不是一个可接受的解决方案,因为所有后续构造的"复合"最大尺寸最终导致一个不合理的大的总对象.
有没有一种方法可以编写这些声明,使编译器接受它,并且所有内容都正确地映射?我可以/应该使用c_ptr
类型吗?我可以/应该摆脱bind(c)
吗?
谢谢你的帮助