Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 7/13/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Mixed-Language Issues

There are important differences in Fortran and C/C++ mixed-language programming; argument passing, naming conventions, and other interface issues must be 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.

A summary of major Fortran and C/C++ mixed-language issues:

  • Generally, Fortran/C programs are mixed to allow one language to use existing code written in the other. Either Fortran or C can call the other, so the main routine can be in either language. On Linux and macOS systems, if Fortran is not the main routine, the -nofor-main compiler option must be specified on the ifort command line that links the application.

  • When the main program is written in Fortran, the Fortran compiler automatically creates any code needed to initialize the Fortran Runtime Library (RTL). The RTL provides the Fortran environment for input/output and exception handling. When the main program is written in C/C++, the C main program needs to call for_rtl_init_ to initialize the Fortran RTL and for_rtl_finish_ at the end of the C main program to shut down the Fortran RTL gracefully. With the Fortran RTL initialized, Fortran I/O and error handling will work correctly even when C/C++ routines are called.

  • For mixed-language applications, the Intel® Fortran main program can call subprograms written in C/C++ if the appropriate calling conventions are used.

    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.

  • On Linux and macOS systems, Fortran adds an underscore to the end of external names; C does not.

  • Fortran changes the case of external names to lowercase on Linux and macOS and to uppercase on Windows; C leaves them in their original case.

  • By default, Fortran passes data by reference; C by value. In both languages, the other method can be specified.

    NOTE:
    It is possible to override some default Fortran behavior by using BIND(C) specifier. This is the preferred method. You can also override default Fortran behavior by using ATTRIBUTES and ALIAS.
  • Fortran subroutines are equivalent to C void routines.

  • Fortran requires that the length of strings is 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 COMPLEX, REAL*16, CHARACTER, derived types, and ALLOCATABLE or array data types, Fortran adds a hidden first argument to contain function return values.

  • On Linux, 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 debugging mixed-language programs on Windows, see Debugging Mixed-Language Programs.