问题
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