Previous: Bridge Routines Up: Mixing Fortran and C Next: Passing Numeric Arguments

Naming Subroutines

The subroutine names for the Fortran and C interfaces must be different. It is not sufficient to add an underscore to the end of a name; the letters themselves must be different. The name difference is forced by the fact that some Fortran compilers put a trailing underscore after the name, and others do not. This is described more fully in Section , Subroutine Names.

In order for a C routine to be called from Fortran, it must be named in a manner the Fortran compiler will recognize. This is handled via the FTN_NAME macro. The macro is used anywhere the subroutine name would be, including in the declaration of the subroutine. The subroutine name is the argument for the macro. Note that ``ftnbridge.h'' must be included. For example:

#include "xvmaininc.h"
#include "ftnbridge.h"

void FTN_NAME(mysub)(param1, param2) int *param1, *param2; /* inputs */ { zmysub(*param1, *param2); }

This would be called by the Fortran routine like this:

      integer a, b
      call mysub(a, b)

A Fortran routine that wishes to be called by C must have a bridge written in C. Otherwise, the C caller would have to use the FTN_NAME macro to call the routine, which would be annoying in the caller's code, plus would cause problems if the routine were ever converted to C. The bridge routine takes care of this. For example:

      subroutine fsub(param1, param2)
      integer param1, param2                   ! inputs
C     do something

The C bridge would look like the following. Note the use of FTN_NAME when calling the Fortran program:

#include "xvmaininc.h"
#include "ftnbridge.h"

void csub(param1, param2) int param1, param2; /* inputs */ { FTN_NAME(fsub)(&param1, &param2); }

It would be called from a C program like this:

int a, b;
csub(a, b);