This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

another calling-fortran-from-C-question


Hi there,
I spent about three days on this now and am quite desperate about it. 

Google is not that much of help, all it reveals: "Don't do it", "Dragons 
there" and similar =(


Nevertheless, I assume I have to do it. The problem:

I would love to use a library written in F90. I managed to compile gfortran 
(latest 4.0.1-prerelease) which in turn compiles the library without a 
glitch. Fine so far.

Now I'd like to have a C-wrapper function to call the library entry point (I 
did this before with code written in f77). 

The function requires arguments as this:
   INTEGER, DIMENSION(:), INTENT(IN) :: RgType
   REAL(kind=stnd), INTENT(IN), DIMENSION(:,:,:) :: Vertices

I learned these arrays are "assumed-shape" arrays, i.e. they can represent 
almost anything. In f77 I could pass a simple C-pointer to fortran routines 
which ask for arguments as in:
   INTEGER, DIMENSION(NumRg), INTENT(IN) :: RgType
   REAL(kind=stnd), INTENT(IN), DIMENSION(DIM,DIM+1,NumRg) :: Vertices


Searching google-groups revealed that each compiler encapsulates these assumed 
shape arrays somehow. I assume it is possible to use some kind of structue in 
C to map the memory layout needed by fortran, maybe similar to:

typedef struct {
  int length;
  void *ptr;
  /* ... */
} f90_assumed_shape_array;

Then, instead of passing
   double x[] = {};
   fortran_function( ..., x, ...);

I could write
   f90_assumed_shape_array x;
   x.length = ...;
   x.ptr    = ...;
   fortran_function( ..., &x, ...);


If so, I'd like to ask for some pointers to docs, sources, whatever where I 
can find information on how gfortran implements this since I don't mind doing 
it non-portably or platform dependent -- everythings better than 
reimplementing that library! I just need the results ...


Thanks in advance for any hint!
	
	Daniel


P.S. If I knew anything about fortran, I could try to replace all those "(:)" 
arrays by the explicit counterparts - but I don't ... and I assume even if I 
would, I would introduce more errors than being helpful.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]