The C++-Pascal Interface |
7 |
![]() |
Sample Interface
You must use the compiler option -lpc when you use CC to link a C++ main routine that calls Pascal. -lpc denotes linking with the Pascal runtime support library libpc. On the Solaris 1.x environment, if you use pc to link, you must add the -lc option.
Compatibility of Types for C++ and Pascal
Table 6-1 and Table 6-2 on page 94 list the default sizes and alignments of compatible types for C and Pascal. They apply to C++ as well.
C++ Name Encoding
To implement function overloading and type-safe linkage, the C++ compiler normally appends type information to the function names. To prevent the C++ compiler from doing so, and to allow Pascal to call a C++ function, declare the C++ function with the extern "C" language construct. One common way to do this is in the declaration of a function, like this:
extern "C" void f (int); |
For brevity, you can also combine extern "C" with the definition of the function, as in:
extern "C" void f (int) |
Procedure Calls: C++-Pascal
Following are examples that illustrate how a C++ main program calls a Pascal procedure. Included in each example are the Pascal procedure, the C++ main program, and the commands to compile and execute the final program.
The Pascal procedure, Samp, in the file, Samp.p
|
procedure Samp (var i: integer; var r: real); |
The C++ main program, SampMain.cc
|
#include <stdio.h> extern "C" void Samp (int&, double&); int main(void) { int i; double d; Samp (i, d); printf ("%d %3.2f \n", i, d); }
|
The commands to compile and execute Samp.p and SampMain.cc
|
hostname% pc -c Samp.p hostname% CC Samp.o SampMain.cc -lpc hostname% a.out 7 3.14
|
Arguments Passed by Reference
C++ arguments can be passed by reference. This section describes how they work with Pascal. Simple Types without the -xl Option
Without the -xl option, simple types match, as in the following example:
The commands to compile and execute SamRef.p and SamRefMain.cc
|
hostname% pc -c SamRef.p hostname% CC SimRef.o SamRefMain.cc -lpc hostname% a.out 00000001 00000000 z 9 9 9.9 9.9
|
Simple Types with the -xl Option
With the -xl option, the Pascal real must be paired with a C++ float; the Pascal integer must be paired with a C++ short int. Strings of Characters
The C++ counterpart to the Pascal alfa and string types are arrays. The C++ counterpart to the Pascal varying type is a structure.
The Pascal procedure, FixVec.p
|
type TVec = array [0..8] of integer; procedure FixVec ( var V: TVec; var Sum: integer ); var i: integer; begin Sum := 0; for i := 0 to 8 do Sum := Sum + V[i]; end;
|
The commands to compile and execute FixVec.p and FixVecMain.cc
|
hostname% pc -c FixVec.p hostname% CC FixVec.o FixVecMain.cc -lpc hostname% a.out 45
|
Although it does not apply to this example, arrays of aggregates in Pascal have, by default, a size that is a multiple of four bytes. When you use the -calign option to compile Pascal code, that difference from C++ is eliminated.
The commands to compile and execute DaysOfWeek.p and DaysOfWeekMain.cc without the -calign option
|
hostname% pc -c DaysOfWeek.p hostname% CC DaysOfWeek.o DaysOfWeekMain.cc -lpc hostname% a.out Day = ''
|
Records and Structures
A Pascal record of an integer and a character string matches a C++ structure of the same constructs, as in this example:
The commands to compile and execute StruChr.p and StruChr.cc
|
hostname% pc -c StruChr.p hostname% CC StruChr.o StruChrMain.cc -lpc hostname% a.out s25 = 'St.Petersburg' strlen (s25) = 13
|
Compile with the -calign option. The program now works correctly.
|
hostname% pc -calign -c DayWeather.p hostname% CC DayWeather.o DayWeatherMain.cc -lpc hostname% a.out day = 'Sunday' weather = 'Sunny'
|
Arguments Passed by Value
C++ arguments can be passed by value. In this section, we describe how they work with Pascal. Simple Types without the -xl Option
Without the -xl option, simple types match, as in the following example:
The commands to compile and execute SimVal.p and SimVal.cc
|
hostname% pc -c SimVal.p hostname% CC SimVal.o SimValMain.cc -lpc hostname% a.out args = 111111
|
Function Return Values
Function return values match types in the same manner as with parameters. They pass in much the same way. Simple Types
Simple types pass in a straightforward way, as in the following example:
The Pascal function, RetReal.p
|
function RetReal (r: real): real; begin RetReal := r + 1 end;
|
The C++ main program, RetRealMain.cc
|
#include <stdio.h> extern "C" double RetReal (double); int main(void) { double r, s; r = 2.0; s = RetReal (r); printf (" %f \n", s); }
|
The commands to compile and execute RetReal.p and RetRealMain.cc
|
hostname% pc -c RetReal.p hostname% CC RetReal.o RetRealMain.cc -lpc hostname% a.out 3.000000
|
The Pascal function, RetShortReal.p
|
function RetShortReal (r: shortreal): shortreal; begin RetShortReal := r + 1.0 end;
|
The commands to compile and execute RetShortReal.p and RetRealMain.cc
|
hostname% pc -c RetShortReal.p hostname% CC RetShortReal.o RetShortRealMain.cc -lpc hostname% a.out 3.000000
|
The Pascal function, IO.p
|
procedure IO;
begin |
The C++ main program, IOMain.cc
|
#include <stdio.h> extern "C" { void IO (); }; int main(void) { IO (); printf ("Hello, C++ ! \n"); }
|
The commands to compile and execute IO.p and IOMain.cc
|
hostname% pc -c IO.p |
Procedure Calls: Pascal-C++
A Pascal main program can also call C++ functions. The following examples show you how to pass simple types and arguments and include the commands that are used to compile and execute the final programs. Arguments Passed by Reference
Pascal arguments can be passed by reference. Here we discuss how they work with C++. Simple Types Passed by Reference
Simple types pass in a straightforward manner, as follows:
The C++ function, SimRef.cc
|
extern "C" void SimRef ( char &t, char &f, char &c, int &i, short &s, float &r, double &d) { t = 1; f = 0; c = 'z'; i = 9; s = 9; r = 9.9; d = 9.9; }
|
The commands to compile and execute SimRef.cc and SimRefMain.p
|
hostname% CC -c SimRef.cc |
Arguments Passed by Value
Pascal arguments can also be passed by value. Here is how they work with C++. Simple Types
Simple types match with value parameters. See the following example:
The commands to compile and execute SimVal.cc and SimValMain.p
|
hostname% CC -c SimVal.cc |
Function Return Values
Function return values match types in the same manner as with parameters. They pass in much the same way.
The C++ function, RetReal.cc
|
extern "C" |
The commands to compile and execute RetReal.cc and RetRealMain.p
|
hostname% CC -c RetReal.cc |
Global Variables in C++ and Pascal
If the types are compatible, a global variable can be shared between C++ and Pascal. See this example:
The Pascal procedure, GloVar.p
|
var Year: integer; procedure GloVar; begin Year := 1995; end;
|
The C++ main program, GloVarMain.cc
|
#include <stdio.h> extern "C" void GloVar (); int Year; int main(void) { Year = 2042; GloVar (); printf (" %d \n", Year); }
|
The commands to compile and execute GloVar.p and GloVarMain.cc
|
hostname% pc -c GloVar.p |
Pascal File Pointers to C++
You can pass a file pointer from Pascal to C++, then have C++ do the I/O. See this example.
The C++ procedure, UseFilePtr.cc
|
#include <stdio.h> extern "C" void UseFilePtr (FILE* ptr) { fprintf (ptr, "[1] \n"); fprintf (ptr, "[2] \n"); fprintf (ptr, "[3] \n"); }
|
The commands to compile and execute UseFilePtr.cc and UseFilePtrMain.p
|
hostname% CC -c UseFilePtr.cc |