background preloader

MIX: C++ and Fortran

Facebook Twitter

R help archive: Re: [R] Barplot legend position. Using C and C++ with Fortran. Last update: Sat Nov 17 16:46:27 2001 Table of contents Background Because of the large existing body of software, particularly numerical software, written in Fortran, it is desirable to call Fortran routines from other languages, notably, C and C++.

Using C and C++ with Fortran

The ISO Fortran committee has tried to work with the ISO C and C++ committees to standardize the interlanguage calling interface, but the latter committees have been unwilling to do so, on the grounds that it would open the door to demands for interfaces to myriad other languages. Thus, there is currently no international protocol for communication between computer programming languages, and one is unlikely to be developed. In practice, this means that interlanguage communication is only possible if supported by operating systems and compilers.

The architecture of DEC VAX (Open)VMS, for example, carefully defined a language-neutral calling sequence, allowing any pair of languages to communicate on that system. Language run-time differences. Tutorial: Using C/C++ and Fortran together. Order of multi dimensional arrays in C/C++ is the opposite of FORTRAN.

Tutorial: Using C/C++ and Fortran together

Native FORTRAN layout (collumn-major order): INTEGER A(2,3) Or INTEGER A(2,2,2) Native C layout (row-major order) is NOT equivalent to the FORTRAN layout: int a[2][3]; Thus: Equivalent multidimension arrays: FORTRAN 77: INTEGER I(2,3,4) FORTRAN 90: INTEGER, DIMENSION(2,3,4) :: I C: int i[4][3][2]; Accessing a FORTRAN array in C: Native FORTRAN: Native C: C array a[3][2] memory layout: In FORTRAN 90, also check out the "RESHAPE" directive. Fortran subroutines are the equivalent of "C" functions returning "(void)".

Note: The entry point names for some FORTRAN compilers have an underscore appended to the name. The f77 comiler flags "-fno-underscore" and "-fno-second-underscore" will alter the default naming in the object code and thus affect linking. Note: The case in FORTRAN is NOT preserved and is represented in lower case in the object file.

Man pages: Character variables: C expects strings to be null-terminated. Mixing Code in C, C++, and FORTRAN on Unix — TTU CAE Network. Major points to keep in mind when mixing languages include: Call By Value / Call By Reference C and C++ default to passing arguments by value; FORTRAN defaults to passing arguments by reference. In other words, the normal way that FORTRAN subroutines or functions are called allows them to modify their argument variables inside the subroutine code, while C and C++ do not. C and C++ subroutines use a slightly different syntax to allow for modification of arguments, and this syntax is consistently used in the following examples. Splitting Code Into Multiple Files Normally, as your program code grows larger, you'll want to separate it into several files, one per function or subroutine.

The advantages of splitting your code up this way include: Once you exceed the two or three source code files we use in the following examples, you'll almost definitely want to investigate using the make utility to automate the build process on your program. Internal Function Names.