Mixed-language programming involves a call from a routine written in one language to a function, procedure, or subroutine written in another language. For example, a Fortran main program may need to execute a specific task that you want to program separately in an assembly-language procedure, or you may need to call an existing shared library or system procedure.
A summary of major Fortran and C/C++ mixed-language issues follows:
Fortran and C implement functions and routines
differently. For example, a C main program could call an external void
function, which is actually implemented as a Fortran subroutine:
Language Equivalents for Calls to Routines
Language |
Call with Return Value |
Call with No Return Value |
Fortran |
FUNCTION |
SUBROUTINE |
C and C++ |
function |
(void) function |
Generally, Fortran/C programs are mixed to allow one to use existing code written in the other language. Either Fortran or C can call the other, so the main routine can be in either language. If Fortran is not the main routine, the -nofor_main compiler option must be specified on the command line.
Fortran adds an underscore to external names; C does not.
Fortran changes the case of external names to lowercase; C leaves them in their original case.
Fortran passes numeric data by reference; C passes by value.
Note
You can override some default Fortran behavior by using ATTRIBUTES and ALIAS. ATTRIBUTES C causes Fortran to act like C in external names and the passing of numeric data. ALIAS causes Fortran to use external names in their original case.
Fortran subroutines are equivalent to C void routines.
Fortran requires that the length of strings be passed; C is able to calculate the length based on the presence of a trailing null. Therefore, if Fortran is passing a string to a C routine, that string needs to be terminated by a null; for example:
"mystring"c or StringVar // CHAR(0)
For the following data types, Fortran adds a hidden first argument to contain function return values: COMPLEX, REAL*16, and CHARACTER.
A summary of Fortran/assembly language issues follows:
Assembly-language routines can be small and can execute quickly because they do not require initialization as do high-level languages like Fortran and C.
Assembly-language routines allow access to hardware instructions unavailable to the high-level language user. In a Fortran/assembly-language program, compiling the main routine in Fortran gives the assembly code access to Fortran high-level procedures and library functions, yet allows freedom to tune the assembly-language routines for maximum speed and efficiency. The main program can also be an assembly-language program.
There are other important differences in languages; for instance, argument passing, naming conventions, and other interface issues must be thoughtfully and consistently reconciled between any two languages to prevent program failure and indeterminate results. However, the advantages of mixed-language programming often make the extra effort worthwhile. The remainder of this section provides an explanation of the techniques you can use to reconcile differences between Fortran and other languages.
Adjusting calling conventions, adjusting naming conventions and writing interface procedures are discussed in the following topics:
After establishing a consistent interface between mixed-language procedures, you then need to reconcile any differences in the treatment of individual data types (strings, arrays, and so on). This is discussed in Exchanging and Accessing Data in Mixed-Language Programming. You also need to be concerned with data types, because each language handles them differently. This is discussed in Handling Data Types in Mixed-Language Programming. Finally, you may need to debug a mixed language programs, as detailed in Debugging Mixed-Language Programs.
Note
This section uses the term "routine" in a generic way, to refer to functions, subroutines, and procedures from different languages.