How to Link Turbo C Files into Turbo Pascal

The basic cookbook to get an .obj file from C that you can
link to TP [switches are in Turbo C, may need modifying for
other C compilers] is

1. If your program uses the RTL library functions recompile 
the RTL first using the switches below.

2. Compile your source code using the switches below.

Switch:       Meaning:
-wrvl         Enable all warnings
-p            Use Pascal calling conventions
-k-           Standard stack frame off
-r            Use the register variables
-u-           Underscore generation is off
-c            Compile to .obj - no .exe file
-ms           Ensure small memory model
-zCCODE       Name the code segment CODE
-zP           Do not create the code group
-zA           Do not create the code class
-zRCONST      Name the static data segment CONST
-zDDATA       Name the data segment DATA
-zS           Do not create the data group
-zT           Do not create the data class
-zG           Do not create the BSS group
-zB           DO not create the BSS class

Notes:

TP places many restrictions on what can be contained in 
the object file to be linked:

a) You must be able to control structure of .obj file 
and any .lib files needed

b) You must control naming conventions for code and 
data segments: code segment *must* be called CSEG or 
CODE and data segment *must* be called DSEG or DATA

c) Object file cannot contain any initialised data 
[ In C this means no initialised statics ]

d) All symbols must have legal Pascal identifier names

e) The object file cannot contain any groups or classes


If you call Pascal code from the C routine, you *MUST* 
save SI and DI in the C code before calling the Pascal 
routine and restore afterwards. A suitable C header file:

#define save_sidi {extern WORD far *_rsp; *_rsp++ = _SI; *_rsp++ = _DI;}
#define rest_sidi {extern WORD far *_rsp; _DI = *--_rsp; _SI = *--_rsp;}

You also need to declare the following in your Pascal program 
if you call Pascal routines from your C source:

var _rsp       : pointer;
var _SiDiStack : array[1..20] of word;

then at the start of the Pascal program you need to make 
the assignment:

  _rsp := @SiDiStack;

Your C code calling a pascal routine will look like
save_sidi;
Pascal_procedure;
rest_sidi;


There are further problems in that C puts constant strings into 
initialised data whereas TP puts them in the code segment.


© Pedt Scragg, Signpost