HP-UX Floating-Point Guide

Chapter 4 123
HP-UX Math Libraries on HP 9000 Systems
Calling C Library Functions from Fortran
Calling C Library Functions from
Fortran
To call a C math library function from a Fortran program, you must do
the following:
1. Use an !$HP$ ALIAS directive (Fortran 90) or an $ALIAS
(FORTRAN 77) directive to tell the compiler that the function’s
arguments are passed by value.
2. Declare the function with the correct return value. See the online
reference pages, Appendix A, or /usr/include/math.h to find the
return value.
3. Link in the C math library (libm).
For example, the following Fortran program calls j0, one of the Bessel
functions in the C math library:
Sample Program: bessel.f
C bessel.f
C %VAL tells the compiler to use C language
C argument-passing conventions
C $HP$ ALIAS directive is for f90, $ALIAS is for f77
C Program declares j0() DOUBLE PRECISION
!$HP$ ALIAS J0 = ‘j0’(%VAL)
C $ALIAS J0 = ‘j0’(%VAL)
PROGRAM BESSEL
DOUBLE PRECISION A, B, J0
A = 1.0
B = J0(A)
WRITE(*,*) “Bessel of”, A, “ is”, B
END
The %val argument indicates that the argument is passed by value. For
details on the !$HP$ ALIAS and $ALIAS directives, see the HP Fortran
90 Programmer’s Reference or the HP FORTRAN/9000 Programmer’s
Guide.