I do not have experience of interfacing Fortran with C.. I mean exchanging and sharing data between C routines and Fortran routines linked together. I would appreciate some guidelines how to do that. Basically, assume the following common block is defined in a fortran routine called "m1505.f": COMMON/LEXP/DLEXP,TO,AINT That is the Fortran routine defined the common block named "LEXP" containing the three valiables DLEXP, TO, AINT Now assume that such a Fortran routine is linked to some C routines and some Tcl/Tk procedures through a GNUmakefile. How can I, from inside a C routine, access the content of the variable AINT in the common block LEXP ? Thank you in advance for any suggestion. Maura
On Tuesday 06 June 2006 4:04 pm, Maura Edeweiss Monville wrote:
I do not have experience of interfacing Fortran with C.. I mean exchanging and sharing data between C routines and Fortran routines linked together. I would appreciate some guidelines how to do that.
Basically, assume the following common block is defined in a fortran routine called "m1505.f": COMMON/LEXP/DLEXP,TO,AINT
That is the Fortran routine defined the common block named "LEXP" containing the three valiables DLEXP, TO, AINT Now assume that such a Fortran routine is linked to some C routines and some Tcl/Tk procedures through a GNUmakefile. How can I, from inside a C routine, access the content of the variable AINT in the common block LEXP ? It's been a while since I have done some mixed code. Essentially, the FORTRAN Common block is roughly equivalent to the C static storage class. In C, you should be able to access them by name. In C, for instance extern double DLEXP;
http://h21007.www2.hp.com/dspp/files/unprotected/Fortran/docs/vf-html/pg/pgw...
However, be very careful. While floating point and integer variables have
the same format, strings, structures, and arrays are quite different.
--
Jerry Feldman
Thak you. Worked examples are always welcome to understand the rules. It's not clear if the C external struct must be embedded in that "pack" declaration. In some other similar websites it is stated that a particular include file must be included in the C module ... This is confusing ... Maura On Tue, 6 Jun 2006, Jerry Feldman wrote:
On Tuesday 06 June 2006 4:04 pm, Maura Edeweiss Monville wrote:
I do not have experience of interfacing Fortran with C.. I mean exchanging and sharing data between C routines and Fortran routines linked together. I would appreciate some guidelines how to do that.
Basically, assume the following common block is defined in a fortran routine called "m1505.f": COMMON/LEXP/DLEXP,TO,AINT
That is the Fortran routine defined the common block named "LEXP" containing the three valiables DLEXP, TO, AINT Now assume that such a Fortran routine is linked to some C routines and some Tcl/Tk procedures through a GNUmakefile. How can I, from inside a C routine, access the content of the variable AINT in the common block LEXP ? It's been a while since I have done some mixed code. Essentially, the FORTRAN Common block is roughly equivalent to the C static storage class. In C, you should be able to access them by name. In C, for instance extern double DLEXP;
http://h21007.www2.hp.com/dspp/files/unprotected/Fortran/docs/vf-html/pg/pgw...
However, be very careful. While floating point and integer variables have the same format, strings, structures, and arrays are quite different.
-- Jerry Feldman
Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9 -- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
On Tuesday 06 June 2006 5:06 pm, Maura Edeweiss Monville wrote:
Thak you. Worked examples are always welcome to understand the rules. It's not clear if the C external struct must be embedded in that "pack" declaration. In some other similar websites it is stated that a particular include file must be included in the C module ... This is confusing ... A C struct can cause many problems: Unpacked in a 64-bit Linux/Unix system: struct { char a; /* starts on a 64-bit boundary */ long b; /* Also starts on a 64-bit boundary */ } s; The size of s is 16 bytes with a 7 byte filler between a and b. On a 32-bit system, a will start on a 32-bit boundary, and b will start on a 32-bit boundary with a 3 byte filler leaving the size of s as 8 bytes. In a packed struct, a will start on a 64-byte boundary assuming it is not included inside a packed structure. But, b will start immediately after a with no filler so the 64-bit version of s is 9 bytes.
Remember that floats (32-bits) and doubles(64-bits) are defined by the IEEE
754 standard (on a Linux and Unix platform). Integers in FORTRAN and C will
be the same on the same platform as long as you understand the difference
in sizes in C and FORTRAN. Structures are another matter as they are
defined by the C standards and the alignments are "implementation defined".
I don't recall if the C standards require variables to be naturally aligned
or not. It does require that a structure start on a natural boundary. And,
as I mentioned, C arrays are row-major and FORTRAN arrays are column major.
Strings in FORTRAN as I remember have a leading length attribute and no
trailing nul.
(I'm going home :-).
--
Jerry Feldman
On Wednesday 07 June 2006 07:43, Jerry Feldman wrote:
On Tuesday 06 June 2006 5:06 pm, Maura Edeweiss Monville wrote:
Thak you. Worked examples are always welcome to understand the rules. It's not clear if the C external struct must be embedded in that "pack" declaration. In some other similar websites it is stated that a particular include file must be included in the C module ... This is confusing ...
A C struct can cause many problems: Unpacked in a 64-bit Linux/Unix system: struct { char a; /* starts on a 64-bit boundary */ long b; /* Also starts on a 64-bit boundary */ } s; The size of s is 16 bytes with a 7 byte filler between a and b. On a 32-bit system, a will start on a 32-bit boundary, and b will start on a 32-bit boundary with a 3 byte filler leaving the size of s as 8 bytes. In a packed struct, a will start on a 64-byte boundary assuming it is not included inside a packed structure. But, b will start immediately after a with no filler so the 64-bit version of s is 9 bytes.
Remember that floats (32-bits) and doubles(64-bits) are defined by the IEEE 754 standard (on a Linux and Unix platform). Integers in FORTRAN and C will be the same on the same platform as long as you understand the difference in sizes in C and FORTRAN. Structures are another matter as they are defined by the C standards and the alignments are "implementation defined". I don't recall if the C standards require variables to be naturally aligned or not. It does require that a structure start on a natural boundary. And, as I mentioned, C arrays are row-major and FORTRAN arrays are column major. Strings in FORTRAN as I remember have a leading length attribute and no trailing nul.
(I'm going home :-).
The first BYTE of a FORTRAN character string is its DEFINED length (not the number of characters stored), and the old standard for calling routines was to pass the number of characters as an extra parameter in a CALL statement (i.e. the length was pushed onto the stack, but not seen in the source code) also, C and FORTRAN parameters were pushed onto the stack in opposite sequence, and clearing the stack can be the responsibility of the caller or of the routine. New FORTRAN compilers are now the same as C. FORTRAN always passes by address. Recent FORTRAN compilers have changed things. So you need to know your compilers :-) Besides COMMON blocks are obsolete now. Use MODULE Good luck. Colin
On Tuesday 06 June 2006 7:42 pm, Colin Carter wrote:
On Wednesday 07 June 2006 07:43, Jerry Feldman wrote:
On Tuesday 06 June 2006 5:06 pm, Maura Edeweiss Monville wrote:
Thak you. Worked examples are always welcome to understand the rules. It's not clear if the C external struct must be embedded in that "pack" declaration. In some other similar websites it is stated that a particular include file must be included in the C module ... This is confusing ...
A C struct can cause many problems: Unpacked in a 64-bit Linux/Unix system: struct { char a; /* starts on a 64-bit boundary */ long b; /* Also starts on a 64-bit boundary */ } s; The size of s is 16 bytes with a 7 byte filler between a and b. On a 32-bit system, a will start on a 32-bit boundary, and b will start on a 32-bit boundary with a 3 byte filler leaving the size of s as 8 bytes. In a packed struct, a will start on a 64-byte boundary assuming it is not included inside a packed structure. But, b will start immediately after a with no filler so the 64-bit version of s is 9 bytes.
Remember that floats (32-bits) and doubles(64-bits) are defined by the IEEE 754 standard (on a Linux and Unix platform). Integers in FORTRAN and C will be the same on the same platform as long as you understand the difference in sizes in C and FORTRAN. Structures are another matter as they are defined by the C standards and the alignments are "implementation defined". I don't recall if the C standards require variables to be naturally aligned or not. It does require that a structure start on a natural boundary. And, as I mentioned, C arrays are row-major and FORTRAN arrays are column major. Strings in FORTRAN as I remember have a leading length attribute and no trailing nul.
(I'm going home :-).
The first BYTE of a FORTRAN character string is its DEFINED length (not the number of characters stored), and the old standard for calling routines was to pass the number of characters as an extra parameter in a CALL statement (i.e. the length was pushed onto the stack, but not seen in the source code) also, C and FORTRAN parameters were pushed onto the stack in opposite sequence, and clearing the stack can be the responsibility of the caller or of the routine. New FORTRAN compilers are now the same as C. FORTRAN always passes by address. Recent FORTRAN compilers have changed things. So you need to know your compilers :-)
Besides COMMON blocks are obsolete now. Use MODULE Thanks for posting this. I know that the code Maura is working with is older existing code. However I did forget to mention the calling sequences. Actually, parameter passing on some systems is defined by a "calling standard". Another thing I forgot to mention is that, as you mentioned, FORTRAN passes parameters by reference where C passes parameters by value. (Note that arrays in C are effectively passed by value). But, as you pointed out it is important to know the platform and compilers. On Linux, while you have GCC and GNU FORTRAN, there are other compilers available. -- Jerry Feldman
Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
participants (3)
-
Colin Carter
-
Jerry Feldman
-
Maura Edeweiss Monville