Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Floating-Point Guide: HP 9000 Computers > Chapter 4 HP-UX Math Libraries on HP 9000 Systems

Calling C Library Functions from Fortran

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

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 “The C Math Library”, 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:

Example 4-3 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.

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 .765197686557967
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1997 Hewlett-Packard Development Company, L.P.