13 Compiling and Linking

Unless they are passing pointers to subprograms, FORTRAN programs do not need to be compiled in any special way when employing mixed language programming since they are not aware that the subprogram that they are calling is not written in FORTRAN. However, when pointers are passed using the mechanism described in Accessing Dynamic Memory from C and FORTRAN (Section 7.3), the FORTRAN code must include the statement:


to define the function CNF_PVAL.


  % cnf_dev

to define the link, CNF_PAR, to the required include file.

When compiling a C function that is to be called from FORTRAN, it should contain the line:

  #include "f77.h"

to define the F77 macros and CNF functions2.

On a Unix system, you can usually tell the C compiler where to look for header files with the -I qualifier to the cc command, e.g.:

  % cc -I/star/include -c func.c

All FORTRAN INCLUDE and C header files for Starlink software are stored in the directory /star/include, and the object files for all Starlink libraries reside in /star/lib.

To link a FORTRAN program prog.f and a C function sub.c with the CNF library, first compile the C function and then compile and link the FORTRAN program:

  % cc -c -I/star/include sub.c
  % f77 prog.f sub.o -L/star/lib ‘cnf_link‘ -o prog

To link a C program prog.c with a FORTRAN subroutine sub.f, the procedure varies depending upon the system being used. It is usually best to try to do the link with the f77 command as the correct FORTRAN libraries will then be searched. However, in some cases there is confusion over the main routine and either cc or ld must be used specifying all the required libraries.

For example, on Alpha/OSF1 it might be:

  % f77 -c sub.f
  % cc prog.c sub.o -L/star/lib ‘cnf_link‘ -lfor -lots -o prog

For ADAM tasks (see SG/4), much of the complication is removed by the task linking scripts alink and ilink which will accept a mixture of FORTRAN and C modules to compile. For example:

  % alink task.f subr.c ...


  % alink task.c subr.f ...

The CNF library will be linked automatically and /star/include searched for any required C header files.

The first program module specified for the ADAM link script must be the main routine of the ADAM task, which is written as a FORTRAN subroutine or C function with a single INTEGER, or int *, argument (see SUN/144 for details).

2The two CNF header files, cnf.h and f77.h are now identical. For legacy reasons it is acceptable to #include either or both in the code – just f77.h is preferred.