|
|
HP C/HP-UX Programmer's Guide: Workstations and Servers > Chapter 5 Programming for Portability Calling Other Languages |
|
It is possible to call a routine written in another language from a C program, but you should have a good reason for doing so. Using more than one language in a program that you plan to port to another system will complicate the process. In any case, make sure that the program is thoroughly tested in any new environment. If you do call another language from C, you will have the other language's anomalies to consider plus possible differences in parameter passing. Since all HP-UX system routines are C programs, calling programs written in other languages should be an uncommon event. If you choose to do so, remember that C passes all parameters by value except arrays and structures. The ramifications of this depend on the language of the called function. Table 5-5 C Interfacing Compatibility
You can compile FORTRAN functions separately by putting the functions you want into a file and compiling it with the -c option to produce a .o file. Then, include the name of this .o file on the cc command line that compiles your C program. The C program can refer to the FORTRAN functions by the names they are declared by in the FORTRAN source. Remember that in FORTRAN, parameters are usually passed by reference (except CHARACTER parameters on Series 700/800, which are passed by descriptor), so actual parameters in a call from C must be pointers or variable names preceded by the address-of operator (&). The following program uses a FORTRAN block data subprogram to initialize a common area and a FORTRAN function to access that area:
The FORTRAN function and block data subprogram contained in file xx.f are compiled using f77 -c xx.f. The C main program is contained in file x.c:
The C main program is compiled using cc -Aa x.c xx.o. Another area for potential problems is passing arrays to FORTRAN subprograms. An important difference between FORTRAN and C is that FORTRAN stores arrays in column-major order whereas C stores them in row-major order (like Pascal). For example, the following shows sample C code:
Here is similar code for FORTRAN:
Therefore, when passing arrays from FORTRAN to C, a C procedure should vary the first array index the fastest. This is shown in the following example in which a FORTRAN program calls a C procedure:
There are other considerations as well when passing arrays to FORTRAN subprograms. It should be noted that a FORTRAN main should not be linked with cc. Pascal gives you the choice of passing parameters by value or by reference (var parameters). C passes all parameters (other than arrays and structures) by value, but allows passing pointers to simulate pass by reference. If the Pascal function does not use var parameters, then you may pass values just as you would to a C function. Actual parameters in the call from the C program corresponding to formal var parameters in the definition of the Pascal function should be pointers. Arrays correlate fairly well between C and Pascal because elements of a multidimensional array are stored in row-major order in both languages. That is, elements are stored by rows; the rightmost subscript varies fastest as elements are accessed in storage order. Note that C has no special type for boolean or logical expressions. Instead, any integer can be used with a zero value representing false, and non-zero representing true. Also, C performs all integer math in full precision (32-bit); the result is then truncated to the appropriate destination size. To call Pascal procedures from C on the HP 9000 workstations and servers, a program may first have to call the Pascal procedure U_INIT_TRAPS. See the HP Pascal Programmer's Guide for details about the TRY/RECOVER mechanism. As true of FORTRAN mains, a Pascal main should not be linked with cc. The following source is the Pascal module:
The command line for producing the Pascal relocatable object is
The command line for compiling the C main program and linking the Pascal module is
The following output results:
|
|