Re: couple c and fortran 95
- From: "PJH" <abc@xxxxxxxxxxx>
- Date: Wed, 6 Dec 2006 23:55:20 -0000
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
.
- References:
- couple c and fortran 95
- From: pradeep.bhattad@xxxxxxxxx
- Re: couple c and fortran 95
- From: glen herrmannsfeldt
- couple c and fortran 95
- Prev by Date: Re: couple c and fortran 95
- Next by Date: Re: Using the same subroutine to read files with different structures
- Previous by thread: Re: couple c and fortran 95
- Next by thread: Re: couple c and fortran 95
- Index(es):
Relevant Pages
|