Previous | Contents | Index |
The properties are described in the following sections.
11.2.2.1 C Property
The C property provides a convenient way for code written in DIGITAL Fortran 90 to interact with routines written in C.
When applied to a subprogram, the C property defines the subprogram as having a specific set of calling conventions.
Table 11-2 summarizes the differences between the calling conventions:
Item | Fortran Default | C Property Specified |
---|---|---|
Trailing underscore added to procedure names | Yes | No |
Case of external subprogram names | Lowercase, unless the ALIAS property is specified | Lowercase, unless the ALIAS property is specified |
Argument passing | See Table 11-3 | See Table 11-3 |
In addition to the case of external names and the trailing underscore, the C property affects how arguments are passed, as described in Table 11-3.
Argument Variable Type | Fortran Default | C Property Specified for Routine |
---|---|---|
Scalar (includes derived types) | Passed by reference | Passed by value |
Scalar, with VALUE specified | Passed by value | Passed by value |
Scalar, with REFERENCE specified | Passed by reference | Passed by reference |
String | Passed by reference with hidden length | String (1:1) padded to integer length |
String, with VALUE specified | Error | String (1:1) padded to integer length |
String, with REFERENCE specified | Passed by reference with no hidden length | Passed by reference with no hidden length |
Arrays, including pointers to arrays | Always passed by reference or descriptor | Always passed by reference or descriptor |
If C is specified for a subprogram, arguments (except for arrays and characters) are passed by value. Subprograms using standard Fortran 90 conventions pass arguments by reference.
Character arguments are passed as follows:
When the C property is specified, the case of the external name (EXTERNAL statement) is forced to lowercase, even if -names as_is or -names uppercase was specified during compilation. To allow mixed case or uppercase names when the C property is specified, specify the ALIAS property for the same subprogram or external name.
Example 11-1 shows the DIGITAL Fortran 90 code that calls the C function pnst (no underscore) by using the cDEC$ ATTRIBUTES C directive and C language passing conventions.
Example 11-1 Calling C Functions and Passing Integer Arguments |
---|
! Using !DEC$ ATTRIBUTES to pass argument to C. File: pass_int_cdec.f90 interface subroutine pnst(i) !DEC$ ATTRIBUTES C :: pnst integer i end subroutine end interface integer :: i i = 99 call pnst(i) ! pass by value print *,"99==",i end |
Example 11-2 shows the C function called pnst (no underscore) that is called by the example program shown in Example 11-1
Example 11-2 Calling C Functions and Passing Integer Arguments |
---|
/* get integer by value from Fortran 90. File: pass_int_cdec_c.c */ void pnst(int i) { printf("99==%d\n",i); i = 100; } |
The files (shown in Example 11-1 and Example 11-2) might be compiled, linked, and run as follows:
% cc -c pass_int_cdec_c.c % f90 -o pass_cdec pass_int_cdec.f90 pass_int_cdec_c.o % pass_cdec 99==99 99== 99 |
You can specify the ALIAS property as cDEC$ ALIAS or as cDEC$ ATTRIBUTES ALIAS; both are equivalent. The ALIAS property allows you to specify that the external name of an external subprogram is different from the name by which the calling procedure references it (see Section 11.2.1).
When both ALIAS and C properties are used for a subprogram or external
(EXTERNAL statement) name, the ALIAS property takes precedence over the
C property. This allows you to specify case-sensitive names (the C
attribute sets them to lowercase).
11.2.2.3 REFERENCE and VALUE Properties
The following cDEC$ ATTRIBUTES properties specify how a dummy argument is to be passed:
When a complex (KIND=4 or KIND=8) argument is passed by value, two floating-point arguments (one containing the real part, the other containing the imaginary part) are passed by immediate value. Conversely, if a COMPLEX parameter to a routine is specified as received by value (or given the C attribute), two REAL parameters are received and stored in the real and imaginary parts of the COMPLEX parameter specified.
Character values, substrings, assumed-size arrays, and adjustable arrays cannot be passed by value. When REFERENCE is specified for a character argument, the string is passed with no length.
VALUE is the default if the C property is specified in the subprogram definition.
Consider the following free-form example, which passes an integer by value:
interface subroutine foo (a) !DEC$ ATTRIBUTES C :: foo integer a end subroutine foo end interface |
This subroutine can be invoked from DIGITAL Fortran 90 using the name foo (no underscore):
integer i i = 1 call foo(i) end program |
This is the actual subroutine code:
subroutine foo (i) !DEC$ ATTRIBUTES C :: foo integer i i = i + 1 . . end subroutine foo |
For More Information:
The EXTERN property specifies that a variable is allocated in another source file. EXTERN can be used in global variable declarations, but it must not be applied on dummy arguments.
You must use EXTERN when accessing variables declared in other languages.
The VARYING directive allows a variable number of calling arguments. If VARYING is specified, the C property must also be specified.
When using the VARYING directive, either the first argument must be a number indicating how many arguments to process, or the last argument must be a special marker (such as -1 ) indicating it is the final argument. The sequence of the arguments, and types and kinds, must be compatible with the called procedure.
For More Information:
See the DIGITAL Fortran Language Reference Manual
11.3 Calling Between DIGITAL Fortran 77 and DIGITAL Fortran 90
On DIGITAL UNIX systems, you can call a DIGITAL Fortran 77 subprogram
from DIGITAL Fortran 90 or call a DIGITAL Fortran 90 subprogram from
DIGITAL Fortran 77 (with a few exceptions). A DIGITAL Fortran 77
procedure and a DIGITAL Fortran 90 procedure can also perform I/O to
the same unit number.
11.3.1 Argument Passing and Function Return Values
The recommended rules for passing arguments and function return values between DIGITAL Fortran 77 and DIGITAL Fortran 90 procedures are as follows:
Example 11-3 and Example 11-4 show passing an array from a DIGITAL Fortran 90 program to a DIGITAL Fortran 77 subroutine that prints its value.
Example 11-3 shows the DIGITAL Fortran 90 program (file array_to_f77.f90 ). It passes the same argument as a target and a pointer. In both cases, it is received by reference by the DIGITAL Fortran 77 subroutine as a target (regular) argument. The interface block in Example 11-3 is not needed, but does allow data type checking.
Example 11-3 DIGITAL Fortran 90 Program Calling a DIGITAL Fortran 77 Subroutine |
---|
! Pass arrays to f77 routine. File: array_to_f77.f90 ! this interface block is not required, but must agree ! with actual procedure. It can be used for type checking. interface ! Procedure interface block subroutine meg(a) integer :: a(3) end subroutine end interface integer, target :: x(3) integer, pointer :: xp(:) x = (/ 1,2,3 /) xp => x call meg(x) ! Call f77 subroutine twice. call meg(xp) end |
Example 11-4 shows the DIGITAL Fortran 77 subprogram called by the DIGITAL Fortran 90 program (file array_f77.f ).
Example 11-4 DIGITAL Fortran 77 Subroutine Called by a DIGITAL Fortran 90 Program |
---|
! Get array argument from F90. File: array_f77.f subroutine meg(a) integer a(3) print *,a end |
These files (shown in Example 11-3 and Example 11-4) might be compiled, linked, and run as follows:
% f77 -c array_f77.f % f90 -o array_to_f77 array_to_f77.f90 array_f77.o % array_to_f77 1 2 3 1 2 3 |
In Example 11-3, because array a is not defined as a pointer in the interface block, the DIGITAL Fortran 90 pointer variable xp is passed as target data by reference (address of the target data).
However, if the interface to the dummy argument had the POINTER attribute, the variable xp would be passed by descriptor. This descriptor would not work with the DIGITAL Fortran 77 program shown in Example 11-4.
For More Information:
To make global data available across DIGITAL Fortran 90 and DIGITAL Fortran 77 procedures, use common blocks.
Common blocks are supported by both DIGITAL Fortran 77 and DIGITAL Fortran 90, but modules are not supported by DIGITAL Fortran 77. Some suggestions about using common blocks follow:
DIGITAL Fortran 90 and DIGITAL Fortran 77 share the same run-time system, so you can perform I/O to the same unit number by DIGITAL Fortran 90 and DIGITAL Fortran 77 procedures. For instance, a DIGITAL Fortran 90 main program can open the file, a DIGITAL Fortran 77 function can issue READ or WRITE statements to the same unit, and the DIGITAL Fortran 90 main program can close the file.
For More Information:
Before creating a mixed-language program that contains procedures written in DIGITAL Fortran 90 and C, you need to know how to:
Use the f90 command (and not cc ) to:
The f90 command passes the appropriate libraries to ld , including the DIGITAL Fortran 90 libraries and libc .
You can use the cc command with the -c option to compile C source files into object files.
For example, the following f90 command compiles and links the DIGITAL Fortran 90 calling main program ex1.f90 and the called C function uopen_.c :
% f90 ex1.f90 uopen_.c |
You can use the cc command to compile the C program into an object file before the f90 command:
% cc -c uopen_.c % f90 ex1.f90 uopen_.o |
When a C program calls a DIGITAL Fortran 90 subprogram, specify the -nofor_main option on the f90 command line:
% cc -c cmain.c % f90 -nofor_main cmain.o fsub.f90 |
To view the preprocessor and compilers used and the libraries passed to ld , use the f90 command -v option.
For More Information:
Previous | Next | Contents | Index |