HPlogo HP C++ Programmer's Guide: HP 9000 Series Workstations and Servers > Chapter 5 Inter-Language Communication

Calling HP Pascal and HP FORTRAN 77 from HP C++

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

This section covers the following topics related to calling HP Pascal and HP FORTRAN 77 from HP C++:

  • the main() function

  • function naming conventions

  • using reference variables to pass arguments

  • using extern "C" linkage

  • strings

  • arrays

  • definition of TRUE and FALSE

  • files

As is the case with calling HP C from HP C++, you must link your application using the C++ driver, CC.

The main() Function

In general, when mixing C++ modules with modules in HP Pascal and HP FORTRAN 77, the overall control of the program must be written in C++. In other words, the main() function must appear in some C++ module.

NOTE: If you wish to have a main() function in a module other than a C++ module, you can add a call to _main() as the first non-declarative statement in the module. However, if you use this method, your code is not portable.

Function Naming Conventions

When calling a HP Pascal or HP FORTRAN 77 function from C++, you must keep in mind the differences between the way the languages handle case sensitivity. HP FORTRAN 77 and HP Pascal are not case sensitive, while the C++ compiling system and the underlying C compiler are case sensitive. Therefore, all C++ global names accessed by FORTRAN 77 or Pascal routines must be lowercase. All FORTRAN 77 and Pascal external names are downshifted by default.

Using Reference Variables to Pass Arguments

There are two methods of passing arguments, by reference or by value. Passing by reference means that the routine passes the address of the argument rather than the value of the argument.

When calling HP Pascal or HP FORTRAN 77 functions from HP C++, you need to ensure that the caller and called functions use the same method of argument passing for each individual argument. Furthermore, when calling external functions in HP Pascal or HP FORTRAN 77, you must know the calling convention for the order of arguments.

It is not recommended that you pass structures or classes to HP FORTRAN 77 or HP Pascal. For maximum compatibility and portability, only simple data types should be passed to routines. All HP C++ parameters are passed by value, as in HP C, except arrays and functions which are passed as pointers.

HP FORTRAN 77 passes all arguments by reference. This means that all actual parameters in an HP C++ call to a FORTRAN routine must be pointers, or variables prefixed with the unary address operator &.

HP Pascal passes arguments by value, unless specified as var parameters. There are two ways to pass variables to Pascal var parameters. One way is to use the address operator &. The other way is to declare the variable as a pointer to the appropriate type, assign the address to the pointer, and pass the pointer.

So, the simplest way to reconcile these differences in argument-passing conventions is to use reference variables in your C++ code. Declaring a parameter as a reference variable in a prototype causes the compiler to pass the argument by reference when the function is invoked. The following example illustrates a reference variable.

int main( void )

{

        // declare a reference variable

     extern void pas_func( short & ); 

     short x;

          .

          .

          .

     pas_func( x );    // pas_func should accept

          .

          .

          .  // its parameters by reference

}

Refer to "References" in The C++ Programming Language for details about using reference variables.

Using extern "C" Linkage

If you want to mix C++ modules with HP FORTRAN 77 or HP Pascal modules, make sure that you use the extern "C" linkage to declare any C++ functions that are called from a non-C++ module and to declare the FORTRAN or Pascal routines.

Strings

HP C++ strings are not the same as HP FORTRAN 77 strings. In FORTRAN 77 the strings are not null terminated. Also, strings are passed as string descriptors in FORTRAN 77. This means that the address of the character item is passed and a length by value follows.

NOTE: On the HP 9000 Series 700/800, the length follows immediately after the character pointer in the parameter list.

HP Pascal strings and HP C++ strings are not compatible. See your HP Pascal manual for details.

Arrays

HP C++ stores arrays in row-major order, whereas HP FORTRAN 77 stores arrays in column-major order. The lower bound for HP C++ is 0. The default lower bound for HP FORTRAN 77 is 1. For HP Pascal, the lower bound may be any user-defined scalar value.

Definition of TRUE and FALSE

On the HP 9000 Series 700/800, HP C++, HP FORTRAN 77, and HP Pascal do not share a common definition of TRUE or FALSE. HP C++ does not have a FORTRAN LOGICAL type. Instead C++ uses integers. In HP C++, any nonzero value is used to represent TRUE and 0 is used to represent FALSE.

HP C++ does not have a Pascal boolean type. On the HP 9000 Series 700/800, HP Pascal allocates 1 byte for boolean variables and only accesses the rightmost bit to determine its value, 1 to represent TRUE and 0 for FALSE.

Files

HP FORTRAN I/O routines require a logical unit number to access a file, whereas HP C++ accesses files using HP-UX I/O subroutines and intrinsics and requires a stream pointer.

A FORTRAN logical unit cannot be passed to a C++ routine to perform I/O on the associated file. Nor can a C++ file pointer be used by a FORTRAN routine. However, a file created by a program written in either language can be used by a program of the other language if the file is declared opened within the latter program. HP-UX I/O (stream I/O) can also be used from FORTRAN instead of FORTRAN I/O.

Refer to your system FORTRAN manual on inter-language calls for details.

A C++ file pointer cannot be passed to a Pascal routine for performing input/output. A Pascal file variable cannot be used by a C++ program. However, a file created by a program written in either language can be used by a program of the other language if the file is declared opened within the latter program.

If I/O from Pascal is required, it is recommended that you use HP-UX input/output routines and intrinsics. This allows C++ and Pascal to use the same I/O mechanism.

See the HP Pascal manual for your system for more details.

Linking HP FORTRAN 77 and HP Pascal Routines on HP-UX

When calling HP FORTRAN 77 or HP Pascal routines on the HP 9000 Series 700/800, you must include the appropriate run-time libraries by adding the following argument to the CC command:

-lcl -lisamstub
© Hewlett-Packard Development Company, L.P.