c-fortran interoperability - derived types with pointers

ぐ巨炮叔叔 提交于 2020-01-02 15:26:30

问题


I have long fortran code that has to be made usable from python. I decided to do Fortran->C->Python interface.

I got a problem: I have derived types within Fortran modules that contain

double precision, allocatable  

type as members.

When trying to compile with ifort I get (with gfortran something similar):

Each component of a derived type with the BIND attribute shall be a nonpointer,         
nonallocatable data component with interoperable type and type parameters

This is actually with agreement with Intel compiler documentation and Fortran 2003 standard: point 15.2.5.

Is there any way to access Fortran "type" with allocatable or pointer inside?


回答1:


Not really. Even the brand new TS 29113 does not offer a solution. It has the TYPE(*) but you have to know, how to decode the structure in C. You can do it by reverse engineering.

There is actually a macro for the size of the structure in the referenced TS. See CFI_attribute_allocatable. But the problem with dereferencing remains. There is a standard array descriptor proposed, but this TS is not yet supported by some compilers (notably by gfortran).

What is possible, is to just pass the pointer to the structure to a generic function such as qsort. Then you use just a piece of memory, that happens to contain also some pointer or allocatable descriptor, but C does not have to know about them.

Another possibility is to construct a derived type which holds only type(c_ptr), instead of Fortran pointers and use c_loc() to fill them. Be sure to have only contiguous arrays then.



来源:https://stackoverflow.com/questions/13748276/c-fortran-interoperability-derived-types-with-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!