Mix Fortran77 & C - F77 & C example

Left
Right

Fortran calling C function

Fortran Main

 
      PROGRAM F2C
C
      CHARACTER*32 NAME
      INTEGER AGE
      REAL TEMP
C
      NAME = "Knut"
C add null character at end for portable & safe handling by C
      NAME(LEN(NAME):LEN(NAME)) = CHAR(0)
C note that LEN(NAME) = 32 in this case
      AGE = 4
      TEMP = 98.6
      CALL NAMEAGE(NAME, AGE, TEMP)
      END
 

C Function

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

void nameage(_fcd name, int *age, float *temp) {
        char *cp;
        size_t len;

        cp = _fcdtocp(name);    /* convert to C char* */
        len = _fcdlen(name);

        /* strip trailing blanks */
        while (cp[len-1] == ' ' || cp[len-1] == '\0') --len;
        printf("Hello %.*s, who is %d years old, "
                "has a temperature of %4.1f\n", len, cp, *age, *temp);
}
 

Compilation Steps

  • Cray - PVPs - UNICOS
    • cc -c f2cfn.c
    • cf77 f2c.f f2cfn.o
    • (or substitute f90 for cf77)
  • IBM - SP2 - AIX
    • xlc -c f2cfn.c
    • xlf f2c.f f2cfn.o
  • SGI - IRIX
    • gcc -c f2cfn.c
    • f77 f2c.f f2cfn.o
  • SUN - SunOS
    • gcc -c f2cfn.c
    • f77 f2c.f f2cfn.o
  • PC - Linux
    • gcc -c f2cfn.c
    • g77 f2c.f f2cfn.o
Left
Right
Slide 6