Previous | Contents | Index |
This chapter contains the following topics:
For information about calling between Compaq Fortran 77 and Compaq Fortran, see Section A.5. |
The bounds of the main program are usually defined by using PROGRAM and END or END PROGRAM statements. Within the main program, you can define entities related to calling a function or subroutine, including modules and interface blocks.
A function or subroutine is considered a subprogram. A subprogram can accept one or more data values passed from the calling routine; the values are called arguments.
There are two types of arguments:
The following methods define the interface between procedures:
An explicit interface occurs when the properties of the subprogram interface are known within the scope of the function or subroutine reference. For example, the function reference or CALL statement occurs at a point where the function or subroutine definition is known through host or use association. Intrinsic procedures also have an explicit interface.
An implicit interface occurs when the properties of the subprogram interface are not known within the scope of the function or subroutine reference. In this case, the procedure data interface is unknown to the compiler. For example, external routines (EXTERNAL statement) that have not been defined in an interface block have an implicit interface.
In most cases, you can use a procedure interface block to make an implicit interface an explicit one. An explicit interface provides the following advantages over an implicit interface:
There are three major types of subprograms:
Procedure interface blocks allow you to specify an explicit interface for a subprogram as well as define generic procedure names. This section limits discussion to those interface blocks used to provide an explicit subprogram interface. For complete information on interface blocks, see the Compaq Fortran Language Reference Manual.
The components of a procedure interface block follow:
For an example of a module that contains a procedure interface block,
see Section 1.3.
11.1.4 Passing Arguments and Function Return Values
Compaq Fortran on Tru64 UNIX and Linux systems uses the same argument-passing conventions as Compaq Fortran 77 on Compaq Tru64 UNIX systems for non-pointer scalar variables and explicit-shape and assumed-size arrays.
When calling Compaq Fortran subprograms, be aware that Compaq Fortran expects to receive arguments the same way it passes them.
The main points about argument passing and function return values are as follows:
The arguments passed from a calling routine must match the dummy arguments declared in the called function or subroutine (or other procedure), as follows:
Certain arguments or function return values require the use of an explicit interface, including assumed-shape dummy arguments, pointer dummy arguments, and function return values that are arrays. This is discussed in the Compaq Fortran Language Reference Manual.
When passing arrays as arguments, the rank and the extents (number of elements in a dimension) should agree, so the arrays have the same shape and are conformable. If you use an assumed-shape array, the rank is specified and extents of the dummy array argument are taken from the actual array argument.
If the rank and extent (shape) do not agree, the arrays are not conformable. The assignment of elements from the actual array to the noncomformable (assumed-size or explicit-shape) dummy array is done by using array element sequence association. Using array element sequence associations is discussed in the Compaq Fortran Language Reference Manual.
Certain combinations of actual and dummy array arguments are disallowed.
Previous sections have discussed the case where the actual and dummy arguments have neither the POINTER attribute nor the TARGET attribute.
The argument passing rules of like type, kind, and rank (for conformable arrays) or array element sequence association (for noncomformable arrays) apply when:
You can specify an actual argument of type POINTER and a dummy argument of type POINTER. You must use an explicit interface that defines the dummy argument with the POINTER attribute in the code containing the actual argument. This ensures that the pointer is passed, rather than the array data itself.
However, if you specify an actual argument of type POINTER and do not specify an appropriate explicit interface (such as an interface block), it is passed as actual (target) data.
When using an explicit interface (by association or procedure interface block), Compaq Fortran will generate a descriptor for the following types of dummy argument data structures:
To allow calling between Compaq Fortran 77 and Compaq Fortran, certain data structure arguments also supported by Compaq Fortran 77 do not use a descriptor, even when an appropriate explicit interface is provided. For example, since explicit-shape and assumed-size arrays are supported by both Compaq Fortran 77 and Compaq Fortran, a descriptor is not used.
However, for cases where the called routine needs the information in the Compaq Fortran descriptor, declare the routine with an assumed-shape or pointer argument and an explicit interface.
The byte components of the Compaq Fortran descriptor follow:
Within the dimension information (24 bytes) for each rank:
For example, consider the following declaration:
integer,target :: a(10,10) integer,pointer :: p(:,:) p => a(9:1:-2,1:9:3) call f(p) . . . |
The descriptor for actual argument p would contain the following values:
When a Compaq Fortran program needs to call a routine written in a different language (or in some cases a Fortran subprogram), there may be a need to use a form other the Compaq Fortran default passing mechanisms. For example, numeric arguments may need to be passed by immediate value instead of by reference.
To change the Compaq Fortran default mechanisms with the Compaq Fortran built-in functions, use the following functions:
The %VAL or %REF functions can only be used as unparenthesized arguments in actual argument lists.
Instead of the Compaq Fortran built-in functions, you can use the cDEC$
ATTRIBUTES directive to change the Compaq Fortran default passing
mechanisms for either an entire call or for individual arguments. (See
Section 11.2.2).
11.1.8.1 Passing Addresses --- %LOC Function
The %LOC built-in function computes the address of a storage element as an INTEGER*8 (Alpha UNIX systems) value. You can then use this value in an arithmetic expression. It has the following form:
%LOC(arg) |
The %LOC function is particularly useful for non-Fortran procedures that may require argument data structures containing the addresses of storage elements. In such cases, the data structures should be declared volatile to protect them from possible optimizations.
The %VAL function passes the argument list entry as a 64-bit immediate value on Compaq Tru64 UNIX and Linux systems. It has the following form:
%VAL(arg) |
The argument-list entry generated by the compiler is the value of the argument (arg). Because argument-list entries are eight bytes long, the argument value must be an INTEGER (including INTEGER*8), LOGICAL (including LOGICAL*8), or REAL (REAL*4 and REAL*8) constant, variable, array element, or expression.
If a COMPLEX (KIND=4) or COMPLEX (KIND=8) argument is passed by value, two REAL arguments (one contains the real part; the other the imaginary part) are passed by immediate value. 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. COMPLEX*32 arguments cannot be passed by value.
If the value is a byte, word, or longword, it is sign-extended to eight bytes.
To produce a zero-extended value rather than a sign-extended value, use
the ZEXT intrinsic function (see the Compaq Fortran Language Reference Manual).
11.1.8.3 Passing Arguments by Reference --- %REF Function
The %REF function passes the argument by reference. It has the following form:
%REF(arg) |
The argument-list entry the compiler generates will contain the address
of the argument, (arg). The argument value can be a record name, a
procedure name, or a numeric or character expression, array, character
array section, or array element. In Compaq Fortran, passing by
reference is the default mechanism for numeric values, so the %REF call
is usually not needed. Passing character arrays with %REF caused the
hidden length to be omitted.
11.1.8.4 Examples of Argument Passing Built-in Functions
The following examples show the use of the argument list built-in functions:
CALL SUB(2,%VAL(2)) |
CHARACTER(LEN=10) A,B CALL SUB(A,%REF(B)) |
INTEGER IARY(20), JARY(20) CALL SUB(IARY,JARY) |
For an example of passing integer data by value (using %VAL) and by
reference (default) to a C function, see Section 11.3.7.
11.2 Using the cDEC$ ALIAS and cDEC$ ATTRIBUTES Directives
This section provides reference information about the following directives:
Use the cDEC$ ALIAS directive to specify that the external name of an external subprogram is different from the name by which the calling procedure references it.
The syntax is:
cDEC$ ALIAS internal-name, external-name |
The internal-name is the name of the subprogram as used in the current program unit.
The external-name is either a quoted character constant (delimited by single quotation marks) or a symbolic name.
If external-name is a quoted character constant, the value of that constant is used as the external name for the specified internal name. The character constant is used as it appears, with no modifications for case or addition or removal of punctuation characters. The default for the Compaq Fortran compiler is to force the name into lowercase and append an underscore unless directed otherwise.
If external-name is a symbolic name, the symbolic name (in lowercase) is used as the external name for the specified internal name and an underscore is appended. Any other declaration of the specified symbolic name is ignored for the purposes of the ALIAS directive.
The Compaq Tru64 UNIX and Linux linker may have restrictions on what appears in an external name and whether external names are case-sensitive.
For example, assume the following program (free source form):
PROGRAM ALIAS_EXAMPLE !DEC$ ALIAS ROUT1, 'ROUT1A' !DEC$ ALIAS ROUT2, 'routine2_' !DEC$ ALIAS ROUT3, rout3A CALL ROUT1 CALL ROUT2 CALL ROUT3 END PROGRAM ALIAS_EXAMPLE |
The three calls are to external routines named ROUT1A, routine2_, and rout3A. Because rout3A is not in quotation marks (character constant), a trailing underscore is added (Compaq Fortran adds a trailing underscore to external names on UNIX systems unless directed otherwise) and the letter A becomes a lowercase a . For details about adding underscores on Linux systems, see Section 11.3.2.
This feature can be useful when porting code in which different routine naming conventions are in use. By adding or removing the cDEC$ ALIAS directive, you can specify an alternate routine name without recoding the application.
You can also use the DECORATE option with the ALIAS option so the external name specified in ALIAS has prefix and postfix decorations performed on it that are associated with the calling mechanism that is in effect.
The cDEC$ ALIAS and cDEC$ ATTRIBUTES ALIAS directives are synonymous.
11.2.2 cDEC$ ATTRIBUTES Directive
Use the cDEC$ ATTRIBUTES directive to specify properties for data objects and procedures. These properties let you specify how data is passed and the rules for invoking procedures. The cDEC$ ATTRIBUTES directive is intended to simplify mixed-language calls with Compaq Fortran routines written in C or Assembler. The STDCALL keyword is synonymous with the C keyword on Compaq Tru64 UNIX and Linux systems.
The cDEC$ ATTRIBUTES directive takes the following form:
cDEC$ ATTRIBUTES att [,att]... :: object [,object]... |
In this form:
c
Is the letter or character (c, C, *, !) that introduces the directive (see the Compaq Fortran Language Reference Manual).att
Is one of the keywords listed in the Compaq Fortran Language Reference Manual. For example, C, ALIAS, REFERENCE, or VALUE.object
Is the name of a data object used as an argument or procedure. Only one object is allowed when using the C, STDCALL, or ALIAS properties.The Compaq Fortran Language Reference Manual shows valid combinations of properties with the various types of objects.
The ATTRIBUTES options C, STDCALL, REFERENCE, VALUE, and VARYING all affect the calling convention of routines.
By default, Fortran passes all data by reference (except the hidden length argument of strings, which is a special case). If the C or STDCALL option is used, the default changes to passing almost all data by value except arrays.
In addition to the calling-convention options C and STDCALL, you can also specify argument options, VALUE and REFERENCE, to pass arguments by value or by reference, regardless of the calling convention option. Arrays can only be passed by reference.
Table 11-1 summarizes the effect of the most common Fortran calling-convention directives.
Arguments | Default | C or STDCALL | C or STDCALL with REFERENCE |
---|---|---|---|
Scalar | Reference | Value | Reference |
Scalar [value] | Value | Value | Value |
Scalar [reference] | Reference | Reference | Reference |
String | Reference, Len: End | String (1:1) | Reference, Len: End |
String [value] | Error | String(1:1) | String(1:1) |
String [reference] | Reference, No Len | Reference: No Len | Reference: No Len |
Array | Reference | Reference | Reference |
Array [value] | Error | Error | Error |
Array [reference] | Reference | Reference | Reference |
Derived type | Reference | Value, size dependent | Reference |
Derived type [value] | Value, size dependent | Value, size dependent | Value, size dependent |
Derived type [reference] | Reference | Reference | Reference |
F90 Pointer | Descriptor | Descriptor | Descriptor |
F90 Pointer [value] | Error | Error | Error |
F90 Pointer [reference] | Descriptor | Descriptor | Descriptor |
The terms used in Table 11-1 mean the following:
Term | Description |
---|---|
[value] | Assigned to the VALUE property |
[reference] | Assigned to the REFERENCE property |
Value | The argument value is pushed on the stack. All values are padded to the next 8-byte boundary. |
Reference | The 8-byte argument address is pushed on the stack. |
Len: End | The length of the string is pushed (by value) on the stack after all of the other arguments. |
No Len | The length of the string is not available to the called procedure. |
String(1:1) |
For string arguments, the first character is converted to
INTEGER(KIND=8) as in ICHAR(string(1:1)) and pushed onto the stack by value. |
Error | Produces a compiler error. |
Descriptor | 8-byte address of the array descriptor. |
Size dependent |
Derived-type arguments specified by value are passed as follows:
|
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 Compaq Fortran 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 95/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 a mixed case or uppercase name when the C property is specified, specify the ALIAS property for the same subprogram or external name.
Example 11-1 shows the Compaq Fortran 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. 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; they 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; neither can REAL*16 and COMPLEX*32 data. 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 Compaq Fortran 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 |
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 to 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.
Before creating a mixed-language program that contains procedures written in Compaq Fortran and C, you need to know how to:
On Linux Alpha systems, this section assumes the usage of the Compaq C
compiler (
ccc
command).
11.3.1 Compiling and Linking Files
Use the f90 or fort command (and not the cc or ccc command) to:
The f90 and fort commands pass the appropriate libraries to ld , including the Compaq Fortran libraries and libc .
You can use the cc and ccc commands with the -c option to compile C source files into object files.
For example, the following f90 command compiles and links the Compaq Fortran calling main program ex1.f90 and the called C function uopen_.c :
% f90 ex1.f90 uopen_.c |
You can use the cc or, on Linux systems, the ccc command to compile the C program into an object file before the f90 command:
% cc -c uopen_.c % f90 ex1.f90 uopen_.o |
The cc ( ccc ) and f90 ( fort ) commands:
When a C program calls a Compaq Fortran 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.
When designing a program that will use Compaq Fortran and C, be aware of the following general rules and available Compaq Fortran capabilities:
You can use a function reference or a CALL statement to invoke a C function from a Compaq Fortran main or subprogram.
If a value will be returned, use a function reference:
C Function Declaration | Compaq Fortran Function Invocation |
---|---|
data-type calc_(
argument-list)
{ ... } ; |
EXTERNAL CALC
data-type :: CALC, variable-name ... variable-name=CALC( argument-list) ... |
If no value is returned, use a void return value and a CALL statement:
C Function Declaration | Compaq Fortran Subroutine Invocation |
---|---|
void calc_(
argument-list)
{ ... } ; |
EXTERNAL CALC
... CALL CALC( argument-list) |
A C main program or function can invoke a Compaq Fortran function or subroutine by using a function prototype declaration and invocation.
If a value is returned, use a FUNCTION declaration:
Compaq Fortran Declaration | C Invocation |
---|---|
FUNCTION CALC(
argument-list)
data-type :: CALC ... END FUNCTION CALC |
extern
data-type calc_(
argument-list)
data-type variable-name; variable-name=calc_( argument-list); ... |
If no value is returned, use a SUBROUTINE declaration and a void return value:
Compaq Fortran Declaration | C Invocation |
---|---|
SUBROUTINE CALC(
argument-list)
... END SUBROUTINE CALC |
extern void calc_(
argument-list)
calc_( argument-list); ... |
Both C and Compaq Fortran pass most function return data by value, but equivalent data types must be used. Table 11-4 lists equivalent function declarations in Compaq Fortran and C. See Table 11-5 for a complete list of data declarations.
C Function Declaration | Compaq Fortran Function Declaration |
---|---|
float rfort_() |
function rfort()
real (kind=4) :: rfort |
double dfort_() |
function dfort()
real (kind=8) :: dfort |
int ifort_() |
function ifort()
integer (kind=4) :: ifort |
Because there are no corresponding data types in C, you should avoid calling Compaq Fortran functions of type REAL (KIND=16), COMPLEX, and DOUBLE COMPLEX, unless for complex data you pass a struct of two float (or double ) C values (see Section 11.3.10).
The Compaq Fortran LOGICAL data types contain a zero if the value is false and a --1 if the value is true, which works with C conditional and if statements.
A character-valued Compaq Fortran function is equivalent to a C language routine with two extra initial arguments added by the Compaq Fortran compiler:
Compaq Fortran follows the argument-passing rules described in Section 11.1.4. These rules include:
Compaq Fortran lets you specify the lengths of its intrinsic numeric data types with the following:
The following declarations of the integer An are equivalent (unless you specified the appropriate f90 command option to override the default integer size):
INTEGER (KIND=4) :: A1 INTEGER (4) :: A2 INTEGER :: A3 INTEGER*4 :: A4 |
Character data in Compaq Fortran is passed and received by address, using an extra hidden-length argument to contain the string length. Dummy character arguments can use assumed-length syntax for accepting character data of varying length.
Consider the following Compaq Fortran subroutine declaration:
SUBROUTINE H (C) CHARACTER(LEN=*) C |
The equivalent C function declaration is:
void h_(char *c, int len); |
The Fortran subroutine can be called from C as follows:
. . . char *chars[15]; h_(chars, 15); |
The calling routine must pass the same number of arguments expected by the called routine. For each argument passed, the manner (mechanism) of passing the argument and the expected data type must match what is expected by the called routine. For instance, C usually passes data by value and Compaq Fortran typically passes argument data by reference (address and, when appropriate, length).
You must determine the appropriate data types in each language that are compatible. When you call a C routine from a Compaq Fortran main program, certain built-in functions may be useful to change the default passing mechanism, as discussed in Section 11.1.8.
If the calling routine cannot pass an argument to the called routine because of a language difference, you may need to rewrite the called routine. Another option is to create an interface jacket routine that handles the passing differences.
When a C program calls a Compaq Fortran subprogram, all arguments must be passed by reference because this is what the Compaq Fortran routine expects. To pass arguments by reference, the arguments must specify addresses rather than values. To pass constants or expressions, their contents must first be placed in variables; then the addresses of the variables are passed.
When you pass the address of the variable, the data types must correspond as shown in Table 11-5 for Compaq Tru64 UNIX systems.
Compaq Fortran Data Declaration | C Data Declaration |
---|---|
integer (kind=2) x | short int x; |
integer (kind=4) x | int x; |
integer (kind=8) x | long int x; __int64 x; |
logical x | unsigned x; |
real x | float x; |
double precision x | double x; |
real (kind=16) x | None 1 |
complex (kind=4) x | struct { float real; float imag } x; |
complex (kind=8) x | struct { double dreal; double dimag } x; |
complex (kind=16) x | struct { long double dreal; long double dimag } x; 1 |
character (len=5) x | char x[5] |
Be aware of the various sizes supported by Compaq Fortran for integer, logical, and real variables (see Chapter 9), and use the size consistent with that used in the C routine.
Compaq Fortran LOGICAL data types contain a zero if the value is false and a --1 if the value is true, which works with C language conditional and if statements.
When one of the arguments is character data, the Compaq Fortran compiler passes the address of the character data as an argument and adds the length of the character string to the end of the argument list (see Section 11.1.4).
When a C program calls a Compaq Fortran subprogram, the C program must explicitly specify these items in an argument list in the following order:
For example, consider the following Compaq Fortran function declaration that returns character data:
character(len=8) function ch() ch = 'ABCDEFG' //CHAR(0) return end |
The following C code invokes the Compaq Fortran function as ch_, explicitly passing the address and length of the character data arguments, as follows:
char s[8] ch_(&s[0],8); |
Any character string passed by Compaq Fortran is not
automatically null-terminated. To null-terminate a string from
Compaq Fortran, use the CHAR intrinsic function (as shown in the
previous example and described in the Compaq Fortran Language Reference Manual).
11.3.7 Example of Passing Integer Data to C Functions
Example 11-3 shows C code that declares the two functions hln_ and mgn_ . These functions display the arguments received. The C language function hln_ expects the argument by value, whereas mgn_ expects the argument by reference (address).
Example 11-3 C Functions Called by a Compaq Fortran Program |
---|
/* get integer by value from Fortran. File: pass_int_to_c.c */ void hln_(int i) { printf("99==%d\n",i); i = 100; } /* get integer by reference from Fortran */ void mgn_(int *i) { printf("99==%d\n",*i); *i = 101; } |
Example 11-4 shows the Compaq Fortran (main program) code that calls the two C functions mgn_ and hln_ .
Example 11-4 Calling C Functions and Passing Integer Arguments |
---|
! Using %REF and %VAL to pass argument to C. File: pass_int_to_cfuncs.f90 integer :: i i = 99 call hln(%VAL(i)) ! pass by value print *,"99==",i call mgn(%REF(i)) ! pass by reference - %REF is optional in this case print *,"101==",i i = 99 call mgn(i) ! pass by reference print *,"101==",i |
The files (shown in Example 11-3 and Example 11-4) might be compiled, linked, and run as follows:
% cc -c pass_int_to_c.c % f90 -o pass_int_to_c pass_int_to_cfuncs.f90 pass_int_to_c.o % pass_int_to_c 99==99 99== 99 99==99 101== 101 99==99 101== 101 |
The following examples show a Compaq Fortran program that calls a C function that serves as an interface (jacket) routine to the setenv library routine (described in setenv(3)).
The Compaq Fortran program is named test_setenv.f .
The C program that contains the setenv_ interface function is named fort_setenv.c .
The Compaq Fortran program performs the following tasks:
Example 11-5 shows the Compaq Fortran program test_setenv.f .
Example 11-5 Compaq Fortran Program Calling a C Function |
---|
! test_setenv.f character(len=50) :: ename, evalue integer :: overwrite, setenv, ret ! Use 3f routine getenv to return PRINTER environment variable value call getenv('PRINTER',evalue) ! Now look at current value write(6,*) 'Previous env. variable value of PRINTER is: ', evalue ! Use setenv C function. Overwrite flag = non-zero means ! overwrite any existing environment variable. ! ! Returns -1 if there was an error in setting the environment variable ename = 'PRINTER' evalue = 'lps40' overwrite = 1 ret = setenv(ename,evalue,overwrite) if (ret < 0) write (6,*) 'Error setting env. variable' ! Now look at current value evalue = ' ' call getenv('PRINTER',evalue) write(6,*) 'New env. variable value of PRINTER is: ', evalue end |
Example 11-6 shows the C program fort_setenv.c .
Example 11-6 C Interface Function Called by Compaq Fortran |
---|
/* fort_setenv.c */ #include <stdlib.h> int setenv_(char *ename,char *evalue,int *overwrite,int ilen1,int ilen2) { int setenv(), lnblnk_(), i1, i2, ow, rc; char *p1, *p2; /* Get string length of each input parameter */ i1 = lnblnk_(ename,ilen1); i2 = lnblnk_(evalue,ilen2); /* Allocate temporary storage */ p1 = malloc((unsigned) i1+1); if( p1 == NULL ) return(-1); p2 = malloc((unsigned) i2+1); if( p2 == NULL ) { free(p1); return(-1); } /* Copy strings, and NUL terminate */ strncpy(p1,ename,i1); p1[i1] = '\0'; strncpy(p2,evalue,i2); p2[i2] = '\0'; ow = *overwrite; /* Call the setenv library routine to set the environment variable */ rc = setenv(p1, p2, ow); free(p1); free(p2); return(rc); } |
The setenv_ function (shown in Example 11-6) sets the environment variable PRINTER to the value lps40 (passed as arguments from the Compaq Fortran calling program) by calling the setenv library routine with the overwrite flag set to 1.
The C function (shown in Example 11-6) uses the passed length to find the last nonblank character and uses the string up to that character. The C variables ilen1 and ilen2 receive the hidden length of the character strings ename and evalue respectively. The C function allocates storage, including an extra byte for the null-terminator to each string. The extra byte is used as part of an argument when calling the setenv library routine.
The following Compaq Fortran code in Example 11-5 calls the setenv_ C function:
ret=setenv(ename,evalue,overwrite) |
This function invocation passes the following arguments to the C function setenv_ :
Compaq Fortran Variable | Purpose | Data Type | How Passed |
---|---|---|---|
ename | Environment variable name | character | by reference, not null-terminated |
evalue | Environment variable string | character | by reference, not null-terminated |
overwrite | Overwrite flag for setenv | integer | by reference |
not declared | Hidden length of ename | integer | by value |
not declared | Hidden length of evalue | integer | by value |
Data passed from Compaq Fortran to C is passed by reference. When passing character data from Compaq Fortran to C, the following rules apply:
The C function (in Example 11-6) is declared as setenv_ and accepts the Compaq Fortran arguments with the following function declaration:
int setenv_(ename,evalue,overwrite,ilen1,ilen2) char *ename, *evalue; int *overwrite; int ilen1, ilen2; |
The status returned from the C function to the calling Compaq Fortran program is an integer passed by value. (The C function obtains this value from setenv library routine.)
To create the executable program, the files might be compiled with the following commands:
% cc -c fort_setenv.c % f90 test_setenv.f fort_setenv.o |
When executed, a.out displays:
% a.out Previous env. variable value of PRINTER is: lpr New env. variable value of PRINTER is: lps40 |
Example 11-7 shows Compaq Fortran code that passes a COMPLEX (KIND=4) value (1.0,0.0) by immediate value to subroutine foo . To pass COMPLEX arguments by value, the compiler passes the real and imaginary parts of the argument as two REAL arguments by immediate value.
Example 11-7 Calling C Functions and Passing Complex Arguments |
---|
! Using !DEC$ATTRIBUTES to pass COMPLEX argument by value to F90 or C. ! File: cv_main.f90 interface subroutine foo(cplx) !DEC$ATTRIBUTES C :: foo complex cplx end subroutine end interface complex(kind=4) c c = (1.0,0.0) call foo(c) ! pass by value end |
If subroutine foo were written in Compaq Fortran, it might look similar to the following example. In this version of subroutine foo , the COMPLEX parameter is received by immediate value. To accomplish this, the compiler accepts two REAL parameters by immediate value and stores them into the real and imaginary parts, respectively, of the COMPLEX parameter cplx.
! File: cv_sub.f90 subroutine foo(cplx) !DEC$ATTRIBUTES C :: foo complex cplx print *, 'The value of the complex number is ', cplx end subroutine |
If subroutine foo were written in C, it might look similar to the following example in which the complex number is explicitly specified as two arguments of type float:
/* File: cv_sub.c */ #include <stdio.h> typedef struct {float c1; float c2;} complex; void foo(complex c) { printf("The value of the complex number is (%f,%f)\n", c.c1, c.c2); } |
The main routine (shown in Example 11-7) might be compiled and linked to the object file created by the compilation of the Compaq Fortran subroutine and then run as follows:
% f90 -o cv cv_main.f90, cv_sub.f90 % cv The value of the complex number is (1.000000,0.0000000E+00) |
The main routine might also be compiled and linked to the object file created by the compilation of the C subroutine and then run as follows:
% cc -c cv_sub.c % f90 -o cv2 cv_main.f90 cv_sub.f90 % cv2 The value of the complex number is (1.000000,0.000000) |
User-defined derived types in Compaq Fortran and user-defined C structures can be passed as arguments if the following conditions are met:
When Compaq Fortran passes scalar numeric data with the pointer attribute, how the scalar numeric data gets passed depends on whether or not an interface block is provided:
When passing scalar numeric data without the pointer attribute, Compaq Fortran passes the actual data by reference. If the called C function declares the dummy argument for the passed data to be passed by a pointer, it accepts the actual data passed by reference (address) and handles it correctly.
Similarly, when passing scalar data from a C program to a Compaq Fortran subprogram, the C program can use pointers to pass numeric data by reference.
Example 11-8 shows a Compaq Fortran program that passes a scalar (nonarray) pointer to a C function. Variable x is a pointer to variable y.
The function call to ifunc1_ uses a procedure interface block, whereas the function call to ifunc2_ does not. Because ifunc1_ uses a procedure interface block (explicit interface) and the argument is given the pointer attribute, the pointer is passed. Without an explicit interface (ifunc2_) , the target data is passed.
Example 11-8 Calling C Functions and Passing Pointer Arguments |
---|
! Pass scalar pointer argument to C. File: scalar_pointer.f90 interface function ifunc1(a) integer, pointer :: a integer ifunc1 end function end interface integer, pointer :: x integer, target :: y y = 88 x => y print *,ifunc1(x) ! interface block visible, so pass ! pointer by reference. C expects "int **" print *,ifunc2(x) ! no interface block visible, so pass ! value of "x" by reference. C expects "int *" print *,y end |
Example 11-9 shows the C function declarations that receive the Compaq Fortran pointer or target arguments from the example in Example 11-8.
Example 11-9 C Functions Receiving Pointer Arguments |
---|
/* C functions Fortran pointers. File: scalar_pointer.c */ int ifunc1_(int **a) { printf("a=%d\n",**a); **a = 99; return 100; } int ifunc2_(int *a) { printf("a=%d\n",*a); *a = 77; return 101; } |
The files (shown in Example 11-8 and Example 11-9) might be compiled, linked, and run as follows:
% cc -c scalar_pointer.c % f90 -o scalar_pointer scalar_pointer.f90 scalar_pointer.o % scalar_pointer a=88 100 a=99 101 77 |
There are two major differences between the way the C and Compaq Fortran languages handle arrays:
Because of these two factors:
Compaq Fortran orders arrays in column-major order. The following Compaq Fortran array declaration for a 2 by 3 array creates elements ordered as y(1,1), y(2,1), y(1,2), y(2,2), y(1,3), y(2,3):
integer y(2,3) |
The Compaq Fortran declaration for a 2 by 3 array can be modified as follows to have the lowest bound 0 and not 1, resulting in elements ordered as y(0,0), y(1,0), y(0,1), y(1,1), y(0,2), y(1,2):
integer y(0:1,0:2) |
The following C array declaration for a 3 by 2 array has elements in row-major order as z[0,0], z[0,1], z[1,0], z[1,1], z[2,0], z[2,1]:
int z[3][2] |
To use C and Compaq Fortran array data:
When passing certain array arguments, if you use an explicit interface that specifies the dummy argument as an array with the POINTER attribute or an assumed-shape array, the argument is passed by array descriptor (see Section 11.1.7).
For information about performance when using multidimensional arrays, see Section 5.5.
Example 11-10 shows a C function declaration for function expshape_ , which prints the passed explicit-shape array.
Example 11-10 C Function That Receives an Explicit-Shape Array |
---|
/* Get explicit-shape arrays from Fortran */ void expshape_(int x[3][2]) { int i,j; for (i=0;i<3;i++) for (j=0;j<2;j++) printf("x[%d][%d]=%d\n",i,j,x[i][j]); } |
Example 11-11 shows a Compaq Fortran program that calls the C function expshape_ (shown in Example 11-10).
Example 11-11 Compaq Fortran Program That Passes an Explicit-Shape Array |
---|
! Pass an explicit-shape array from Fortran to C. integer :: x(2,3) x = reshape( (/(i,i=1,6)/), (/2,3/) ) call expshape(x) end |
The files (shown in Example 11-10 and Example 11-11) might be compiled, linked, and run as follows:
% cc -c exparray.c % f90 -o exparray exparray.f90 exparray.o % exparray x[0][0]=1 x[0][1]=2 x[1][0]=3 x[1][1]=4 x[2][0]=5 x[2][1]=6 |
For information on the use of array arguments with Compaq Fortran, see
Section 11.1.5.
11.3.13 Handling Common Blocks of Data
The following notes apply to handling common blocks of data between Compaq Fortran and C:
The following examples show how C and Compaq Fortran code can access common blocks of data. The C code declares a global structure, calls the f_calc_ Compaq Fortran function to set the values, and prints the values:
struct S {int j; float k;}r_; main() { f_calc_(); printf("%d %f\n", r_.j, r_.k); } |
The Compaq Fortran function then sets the data values:
subroutine f_calc() common /r/j,k real k integer j j = 356 k = 5.9 return end |
The C program then prints the structure member values 356 and 5.9 set by the Compaq Fortran function.
The previous example applies to Compaq Tru64 UNIX systems. On Compaq
Linux systems, the external names with one underscore would have two
trailing underscores.
11.4 Calling Between Parallel HPF and Non-Parallel HPF Code
When calling between parallel HPF and non-parallel HPF code (TU*X ONLY), the -hpf and -nohpf_main compile-time options are required in certain cases and prohibited in other cases.
Previous | Next | Contents | Index |