Re: F90 to C interface



"Tim Springer" <Tim.Springer@xxxxxxxxxx> writes:

In case the answer to this first question in "no" can anybody give me
some hints and tips on how a F90 interface to a C-library should
look!? I am an experienced fortran programmer (many years with F77 and
the last 3 years with F90) but I have little to no knowledge on C. So
any hints/tips/pointers/examples are welcome!

I had a while ago a similar problem (interfacing Fortran code with C
libraries); theoretically you would be able to use the Fortran2003
C-interop stuff (especially C_F_POINTER), but the compiler support at
the moment is ... sparse; g95 and gfortran have some capabilities, Sun's
f95 has a usable ISO_C_BINDING module which I had used (but had to ditch
the Sun compiler because MVAPICH/VAPI wasn't compilable with it).

In the end I had written a simple wrapper routine in C for every used
library function which has a signature like

void foo_ (<type>* a, <type>* b, ..., <type>* status) { ... }

and calls the library function "foo" with pointers dereferenced where
necessary. A Fortran module was also written containing interface
definitions for the wrapper routines.

Regarding arrays, you have to be careful: onedimensional continuous
arrays ("vectors") can be passed as an assumed-size array (like C does
it), but for arrays with rank 2 or more, you either have to use the
C-interop capabilities where available, try your luck with the CHASM
library or resort to Cray pointers (what I was forced to do).

On a few occasions it was helpful to look at the source of any MPI
library for some common problems and solutions.


Sebastian
.



Relevant Pages

  • Re: couple c and fortran 95
    ... I would always put middleware layer at either side of the interface not just ... Fortran by Reference. ... arrays or by passing a pointer from the C side of the interface. ... C strings are null terminated arrays of character; ...
    (comp.lang.fortran)
  • Re: How old is the average Fortran programmer?
    ... No. C++ has many advantages over Fortran: ... metaprogramming, libraries etc. ... If your problem can be solved elegantly by looping over arrays of floats ... specialized numerics skills such as array syntax are thus left to ...
    (comp.lang.fortran)
  • Re: I dont have Matlab but I need the data contained with a .mat file.
    ... If I can find the right libraries, I might be able to "roll-my-owe" with a little less pain using some code I ran across at The Code Project webpage. ... The arrays contained within the .mat file are stock exchange data used in an article on a local search algorithm. ... I'll read the ASCII text files containing the arrays into Fortran and code my algorithm in Fortran. ...
    (comp.soft-sys.matlab)
  • Re: I dont have Matlab but I need the data contained with a .mat file.
    ... If I can find the right libraries, ... ran across at The Code Project webpage. ... I'll read the ASCII text files containing the arrays into Fortran ...
    (comp.soft-sys.matlab)
  • Re: Merits of fortran
    ... Fortran right now), and I disagree with that statement. ... nothing that's as natural to use as Fortran arrays -- and what it does ... support arrays per se - one can only manipulate a pointer to a block ... Then there are many other libraries that define array- and matrix-like ...
    (comp.lang.fortran)