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
.
Type:
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:
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.:
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:
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:
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:
or
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.