HP Fortran Programmer's Guide (B3908-90031; September 2011)

Calling C routines from HP Fortran
Case sensitivity
Chapter 8190
2. The function expects its second argument (the size of the array) to be passed by value.
The following $HP$ ALIAS directive handles both issues:
!$HP$ ALIAS bubblesort = 'BubbleSort'(%REF, %VAL)
The name bubblesort is the alias that Fortran will use to refer to the C function, and the %REF and %VAL
built-in functions change Fortran’s argument-passing conventions to conform to how the C function expects
the arguments to be passed.
The following is an HP Fortran program that uses the $HP$ ALIAS directive to call the C function correctly.
Example 8-4 test_sort.f90
PROGRAM main
! This program is linked with an object file that contains
! a C function with the following prototype declaration:
!
! void BubbleSort(int a[], int size);
!
! The ALIAS directive takes care of the differences
! between C and Fortran regarding case sensitivity
! and argument-passing conventions.
!$HP$ ALIAS bubblesort = 'BubbleSort'(%REF, %VAL)
INTEGER, PARAMETER :: n = 10
INTEGER, DIMENSION(n) :: num=(/5,4,7,8,1,0,9,3,2,6/)
PRINT *, 'Before sorting: ', num
CALL bubblesort(num, n)
PRINT *, 'After sorting: ', num
END PROGRAM main
Here are the command lines to compile, link, and execute the program, followed by the output from a
sample run:
$ cc -Aa -c sort_em.c
$ f90 test_sort.f90 sort_em.o
$ a.out
Before sorting: 5 4 7 8 1 0 9 3 2 6
After sorting: 0 1 2 3 4 5 6 7 8 9
If you use the $HP$ ALIAS directive in many of the Fortran source files in your program, you may find it
convenient to define all of the directives in one file and include that file in all of the Fortran source files with
the +pre_include=file option. This option takes one argument, file, which is the name of the file you want
to include. All text in file is prepended to each of the source files specified on the command line, before
being passed to the compiler.