Mix Fortran77 & C - F77 & C example

Left
blank

C calling Fortran subroutine

C Main

 
#include <string.h>
#ifdef _CRAY
#  include <fortran.h>
#  define nameage      NAMEAGE
#else
#  ifndef _AIX
#    define nameage    nameage_
#  endif
#  define _fcd          char *
#  define _cptofcd(a,b) (a)
#  define _fcdlen(a)    strlen(a)
#endif

void nameage(_fcd name, int *nlen, int *age, float *temp);

int main() {
        char *name = "Knut";
        _fcd fp;
        int nlen,age = 4;
        float temp = 98.6;

        nlen = strlen(name);
        fp = _cptofcd(name, nlen);      /* convert to Fortran string */

        nameage(fp, &nlen, &age, &temp);
        return 0;
}
 

Fortran Subroutine

 
      SUBROUTINE NAMEAGE(NAME, NLEN, AGE, TEMP)
      CHARACTER*(*) NAME
      INTEGER NLEN,AGE
      REAL TEMP
C
      WRITE(6,1000) NAME(1:NLEN),AGE,TEMP
 1000 FORMAT(1X,'Hello ',A,', who is ',I2,
     .      ' years old, has a temperature of ', f4.1)
      RETURN
      END
 

Compilation Steps

To discover which libraries are necessary for Fortran modules, compile a short Fortran test program and add the -V option (or equivalent) to get a verbose execution listing.
  • Cray - PVPs - UNICOS
    • cf77 -c c2ffn.f
    • cc -c c2f.c
    • cf77 c2f.o c2ffn.o
    • (or substitute f90 for cf77)
  • IBM - SP2 - AIX
    • xlf -c c2ffn.f
    • cc c2f.c c2ffn.o -lxlf90 -lxlf -lm
  • SGI - IRIX
    • f77 -c c2ffn.f
    • gcc c2f.c c2ffn.o -lF77 -lm -lU77 -lI77 -lisam -lmpc -lc
  • SUN - SunOS
    • f77 -c c2ffn.f
    • gcc c2f.c c2ffn.o -L/usr/lang/SC0.0 -lF77 -lm -lc
  • PC - Linux
    • g77 -c c2ffn.f
    • gcc -c c2f.c
    • g77 c2f.o c2ffn.o
Left
blank
Slide 7