Adjusting Calling Conventions in Mixed-Language Programming Overview
The calling convention determines how a program makes a call to a routine,
how the arguments are passed, and how the routines are named.
A calling convention includes:
-
Stack considerations
- Does a routine receive a varying or fixed number
of arguments?
- Which routine clears the stack after a call?
-
Naming conventions
- Is lowercase or uppercase significant or not
significant?
- Are external names altered?
-
Argument passing protocol
- Are arguments passed by value or by reference?
- What are the equivalent data types and data structures
among languages?
In a single-language program, calling conventions are nearly always
correct, because there is one default for all routines and because header
files or Fortran module files with interface blocks enforce consistency
between the caller and the called routine.
In a mixed-language program, different languages cannot share the same
header files. If you link Fortran and C routines that
use different calling conventions, the error is not apparent until the
bad call is made at run time. During execution, the bad call causes indeterminate
results and/or a fatal error. The error, caused by memory or stack corruption
due to calling errors, often occurs in a seemingly arbitrary place in
the program.
The discussion of calling conventions between languages applies only
to external procedures. You cannot call internal procedures from outside
the program unit that contains them.
A calling convention affects programming in a number of ways:
- The caller routine uses a calling convention to determine
the order in which to pass arguments to another routine; the called routine
uses a calling convention to determine the order in which to receive the
arguments passed to it. In Fortran, you can specify these conventions
in a mixed-language interface with the INTERFACE
statement or in a data or function declaration. C/C++ and Fortran both
pass arguments in order from left to right.
- The following is
applies to Windows only: The caller routine and the called routine
use a calling convention to determine which of them is responsible for
adjusting the stack in order to remove arguments when the execution of
the called routine is complete. You can specify these conventions with
ATTRIBUTES (cDEC$ ATTRIBUTES
compiler directive) options such as C or STDCALL.
- The caller routine and the called routine use a calling
convention to select the option of passing a variable number of arguments.
- The caller routine and the called routine use a calling
convention to pass arguments by value (values passed) or by reference
(addresses passed). Individual Fortran arguments can also be designated
with ATTRIBUTES option VALUE or REFERENCE.
- The caller routine and the called routine use a calling
convention to establish naming conventions for procedure names. You can
establish any procedure name you want, regardless of its Fortran name,
with the ALIAS
directive (or ATTRIBUTES option ALIAS).
This is useful because C is case sensitive, while Fortran is not.