Fortran接口

Chapter 11

C-Fortran Interface

This chapter treats issues regarding Fortran and C interoperability.

The discussion is inherently limited to the specifics of the Sun FORTRAN 77, Fortran 95, and C compilers.

 


Note – Material common to both FORTRAN 77 and Fortran 95 is presented in examples that use FORTRAN 77.


Compatibility Issues

Most C-Fortran interfaces must agree in all of these aspects:

    • Function/subroutine: definition and call
  • Data types: compatibility of types
  • Arguments: passing by reference or value
  • Arguments: order
  • Procedure name: uppercase and lowercase and trailing underscore (_)
  • Libraries: telling the linker to use Fortran libraries

Some C-Fortran interfaces must also agree on:

    • Arrays: indexing and order
  • File descriptors and
  • stdio
  • File permissions

Function or Subroutine?

The word function has different meanings in C and Fortran. Depending on the situation, the choice is important:

    • In C, all subprograms are functions; however, some may return a null (void) value.
  • In Fortran, a function passes a return value, but a subroutine does not.

When a Fortran routine calls a C function:

    • If the called C function returns a value, call it from Fortran as a function.
  • If the called C function does not return a value, call it as a subroutine.

When a C function calls a Fortran subprogram:

    • If the called Fortran subprogram is a function, call it from C as a function that returns a compatible data type.
  • If the called Fortran subprogram is a subroutine, call it from C as a function that returns a value of int (compatible to Fortran INTEGER*4) or void. A value is returned if the Fortran subroutine uses alternate returns, in which case it is the value of the expression on the RETURN statement. If no expression appears on the RETURN statement, and alternate returns are declared on SUBROUTINE statement, a zero is returned.

Data Type Compatibility

The tables below summarize the data sizes and default alignments for FORTRAN 77 and Fortran 95 data types. In both tables, note the following:

    • C data types int, long int, and long are equivalent (4 bytes). In a 64-bit environment and compiling with -xarch=v9 or v9a, long and pointers are 8 bytes. This is referred to as "LP64".
  • REAL*16 and COMPLEX*32, (REAL(KIND=16) and COMPLEX(KIND=16)), are available only on SPARC platforms. In a 64-bit environment and compiling with -xarch=v9 or v9a, alignment is on 16-byte boundaries.
  • Alignments marked 4/8 for SPARC indicate that alignment is 8-bytes by default, but on 4-byte boundaries in COMMON blocks. The maximum alignment in COMMON is 4-bytes.
  • The elements and fields of arrays and structures must be compatible.
  • You cannot pass arrays, character strings, or structures by value.
  • You can pass arguments by value from f77 to C, but not from C to f77, since %VAL() is not allowed in a Fortran dummy argument list.
FORTRAN 77 and C Data Types

TABLE 11-1 shows the sizes and allowable alignments for FORTRAN 77 data types. It assumes no compilation options affecting alignment or promoting default data sizes are applied. (See also the FORTRAN 77 Language Reference Manual).

TABLE 11-1   Data Sizes and Alignments--(in Bytes) Pass by Reference (f77 and cc)
FORTRAN 77 Data Type C Data Type Size Default Alignment
SPARC     x86
BYTE X
CHARACTER X
CHARACTER*
n X
char x
unsigned char x
unsigned char x[n]
1
1
n
1
1
1
1
1
1
COMPLEX X
COMPLEX*8 X
DOUBLE COMPLEX X
COMPLEX*16 X
COMPLEX*32 X
struct {float r,i;} x;
struct {float r,i;} x;
struct {double dr,di;}x;
struct {double dr,di;}x;
struct {long double dr,di;} x;
8
8
16
16
32
4
4
4/8
4/8
4/8/16
4
4
4
4
--
DOUBLE PRECISION X
REAL X
REAL*4 X
REAL*8 X
REAL*16 X
double x
float x
float x
double x
long double x
8
4
4
8
16
4/8
4
4
4/8
4/8/16
4
4
4
4
--
INTEGER X
INTEGER*2 X
INTEGER*4 X
INTEGER*8 X
int x
short x
int x
long long int x
4
2
4
8
4
2
4
4
4
2
4
4
LOGICAL X
LOGICAL*1 X
LOGICAL*2 X
LOGICAL*4 X
LOGICAL*8 X
int x
char x
short x
int x
long long int x
4
1
2
4
8
4
1
2
4
4
4
1
2
4
4

 

 

 

SPARC: Fortran 95 and C Data Types

The following table similarly compares the Fortran 95 data types with C.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值