Re: couple c and fortran 95



To add a few extra points to Glen's list.

I would always put middleware layer at either side of the interface not just
on the C side. This will allow you to package and unwrap arguments in a
consistemt way.

There are many differences between C and Fortran passing conventions the
most significant ones being:
C usually passes scalars by value. Fortran by Reference. If your compiler
won't help you sort this out, you can get around it by packing scalars into
arrays or by passing a pointer from the C side of the interface.
C strings are null terminated arrays of character; Fortran strings have a
hidden length parameter, that may be next to the character argument or
collected at the end of the argument list. My advice is to use the
null-terminated strings on the interface - it is reasonably simple to write
a bit of Fortran to convert a string to a null terminated character array.
C arrays start at 0 - in Fortran they generally start at 1. Always pass an
array length argument with an array and in the called procedure check this
is adequate to hold all array return data.
Multi-dimensioned arrays are packed differently (Fortran is Column Major, C
is Row Major). My advice is to unpack arrays to rank 1 to pass across the
interface.
Depending on how you are linking the object code, you might find some issues
with different procedure name decoration. I am only familiar with Win32
systems so cannot advise on your environement.

The best general advice I can give is Keep It Simple. In particular.
Don't try to pass data structures, common or logicals - convert them into
simpler types.
Always use subroutines rather than functions
The following document is as good a reference as I have found on C-Fortran
mixed language programming.
http://www.math.utah.edu/software/c-with-fortran.html

Paul Holden


pradeep.bhattad@xxxxxxxxx wrote:

I have a small program written in c, can i invoke that c program in my
fortran 95??
The c program reads the data from a file in specific format, and
calculations are done in fortran 95 (I need a fix because i have lots
of these c files written by someone else, and changing all will take
long time)

"glen herrmannsfeldt" wrote

If the C program expects pointers it should be easy to call from
Fortran. Fortran does not normally pass by value, which is required
for C arguments that are not pointers. (Some compilers may have
an option for it.)

Otherwise, you might have to write an intermediate C routine accepting
a pointer to a variable and passing the value to the desired routine.

There are also some complications for CHARACTER arguments that might
be system dependent.

-- glen


.



Relevant Pages

  • Re: F90 to C interface
    ... I had a while ago a similar problem (interfacing Fortran code with C ... libraries); theoretically you would be able to use the Fortran2003 ... A Fortran module was also written containing interface ... Regarding arrays, you have to be careful: ...
    (comp.lang.fortran)
  • Several questions about character C binding
    ... I have many questions about character variables passed to a C ... I tested an interface passing a TYPE. ... obligatory the use of the C_LOC function from the FORTRAN side: ... the dimension of the string behind the scene as an additional argument ...
    (comp.lang.fortran)
  • Issue with table parameter types and varchar column in C++ ODBC
    ... Connection may have been terminated by the server." ... It occurs on the 'second' SP call to the interface using the same connection ... The first loop SP execution does succeed and character arrays ...
    (microsoft.public.data.odbc)
  • Re: rank mismatch
    ... > You can write two versions yet reference them via a generic name ("Fortran ... end interface f_c0 ... for rank 1 arrays. ...
    (comp.lang.fortran)
  • Re: best practices for structuring ASCII input
    ... I'm maintaining a number of smallengineering/scientific Fortran ... data consists of a mixture of variables, arrays, ... the input files are not human readable. ...
    (comp.lang.fortran)