Unlike HP Fortran 90, C is a case-sensitive
language. HP Fortran 90 converts all external
names to lowercase, and it disregards the case of internal names.
Thus, for example, the names foo
and FOO are the
same in Fortran. C, however, is a case-sensitive language: foo
and FOO are different in C. If
an HP Fortran 90 program is linked to a C object
file and references a C function that uses uppercase characters
in its name, the linker will not be able to resolve the reference.
If case sensitivity is an issue when calling a C function
from an HP Fortran 90 program, you have two choices:
Compile the Fortran program with the
+uppercase option,
which forces Fortran to use uppercase for external names.
Use the $HP$ ALIAS
directive to specify the case that Fortran should use when calling
an external name.
It is unusual that all names in the C source file would be
uppercase, which would be the only case justifying the use of the
+uppercase option.
Therefore, we recommend using the $HP$ ALIAS
directive. This directive enables you to associate an external name
with an external name, even if the external name uses uppercase
characters.
The $HP$ ALIAS directive
also has the advantage that you can use it with the %REF
and %VAL built-in functions to
specify how the arguments are to be passed without having to repeat
them at every call site.
Consider the following C source file, which contains a function
to sort an array of integers:
Example 8-3 sort_em.c
#include <stdio.h> void BubbleSort(int a[], int size) { int i, j, temp; for (i = 0; i < size - 1; i++) for (j = i + 1; j < size; j++) if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } |
Before a Fortran program can call this function correctly,
it must resolve two issues:
The name of the C function contains
both uppercase and lowercase letters.
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.
Following is an HP Fortran 90 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.
See “File handling ” for
another example of a program that uses the $HP$ ALIAS
directive. The HP Fortran 90 Programmer's Reference
fully describes the %VAL and %REF
built-in functions, the +uppercase
and +pre_include options. Th e
$HP$ ALIAS directive
is discussed in “$HP$ ALIAS”.