FORTRAN77 Program Calls C++ Function

F77_CALLS_C++ is a directory which demonstrates how a FORTRAN77 program can call a C++ function in a way that might work; because FORTRAN77 is limited in some ways, it actually can be difficult or impossible to call a C++ function, especially if the interface to that C++ function is not under the control of the user.

Some reference books discuss this topic by showing little isolated pieces of code, which do not form a real program, do not do anything "interesting", and cannot actually be compiled or used. Since EVERYTHING has to be correctly written and working properly together to make this delicate operation happen, it is very important to have a complete calculation in mind, and to be able to examine the full FORTRAN77 and C++ source code, as well as the compile and load statements used.

The KRONROD example presented here involves a FORTRAN77 main program and a library of 4 C++ routines. For comparison, you can also look at a directory where the same calculation is done with the main program and library routines written in the same language. Simply go to the KRONROD directory for the language you are interested in.

Perhaps the trickiest issue here is the difference in conventions for dealing with a scalar argument passed to a function. A C++ function will typically expect such an argument to be passed by value; it is also possible to pass the quantity by reference, although this is more unusual. If the C++ function already exists and cannot be changed by the user, then this can present a difficulty. The reason is that FORTRAN77 only provides the option of passing scalar arguments by reference.

VAX FORTRAN, and the GFORTRAN compiler provide a function %VAL(variable) which essentially instructs the compiler to use call-by-value instead of call-by-reference for the indicated argument. In other words, a FORTRAN program might call a C++ function with the statement

kronrod ( %val ( n ), %val ( eps ), x, w )
where n and eps are scalar values, while x and w are presumably vectors for which call-by-reference is appropriate and typically used automatically by both C++ and FORTRAN.

If your FORTRAN77 compiler does not provide some way to override the call-by-reference convention, then it can be almost impossible to properly communicate data directly between a FORTRAN calling program and a C++ function.

The C++ functions used here are quite simple, and essentially are really C functions. In fact, this same example is available with the functions written explicitly in C. The differences between that C example and this C++ version are simply

This example does not show how to pass data that needs to have the form of a structure. It also does not deal with classes.


The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.


F77_CALLS_C++ is available in a FORTRAN77 version and a FORTRAN90 version and a MATLAB version.

Related Data and Programs:

C++_CALLS_F77, C++ programs which call a FORTRAN77 subroutine.

F77_CALLS_C, FORTRAN77 programs which call a C function.

F90_CALLS_C++, FORTRAN90 programs which call a C++ function.

MATLAB_CALLS_C++, MATLAB programs which illustrate how C++ functions can be written, compiled, and called from MATLAB using the MEX facility;

MIXED, FORTRAN77 programs which call a function written in another programming language.


Source Code:

The KRONROD example involves a FORTRAN77 main program which calls directly the C++ functions "kronrod" and "timestamp".

You can go up one level to the FORTRAN77 source codes.

Last revised on 14 March 2014.