To call a C math library function from a Fortran program,
you must do the following:
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.
Declare the function with the correct return value.
See the online reference pages, Appendix A “The C Math Library”, or /usr/include/math.h
to find the return value.
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:
Example 4-4 Sample Program: bessel.f
C $HP$ ALIAS directive tells the compiler to use C language C argument-passing conventions (Fortran 90) C $ALIAS is f77 version 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.
You can compile and run the program as follows:
$ f90 bessel.f -lm bessel.f program BESSEL 16 Lines Compiled $ ./a.out Bessel of 1.0 is .7651976865579666 |