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.
Mixed-language programming is possible with Intel® Fortran, Visual C/C++*, and Intel® C++, because each language implements functions, subroutines, and procedures in approximately the same way.
Intel Fortran includes several Fortran 2003 features that provide interoperability with C. An entity is considered interoperable if equivalent declarations can be made for it in both languages. Interoperability is provided for variables, derived types, and procedures. The following features are supported:
BIND attribute and statement, which specifies that an object is interoperable with C and has external linkage.
language binding in FUNCTION and SUBROUTINE statements
language binding in derived-type statements
For more information, see Interoperability with C.
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. On Linux and Mac OS systems, if Fortran is not the main routine, the -nofor-main compiler option must be specified on the command line.
To use the same Microsoft* visual development environment for multiple languages, you must have the same version of the visual development environment for your languages.
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.
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.
On Linux* systems, the -fexceptions option enables C++ exception handling table generation, preventing Fortran routines in mixed-language applications from interfering with exception handling between C++ routines.
For more information on mixed language programming using Intel Fortran and C, see the following:
Compiling and Linking Intel Fortran/C Programs
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.
This section uses the term "routine" in a generic way, to refer to functions, subroutines, and procedures from different languages.