PowerBASIC Frequently Asked Question
Last Update: August 12, 1995
Question:
"How do I use external 'C' routines with my PowerBASIC code?"
Answer:
Using external "C" routines with PowerBASIC code is fairly straightforward
if the following steps are followed.
==============================================================================
=
Part I: Using Borland C, C++, etc...
==============================================================================
=
1. Parameter passing conventions
--------------------------------
The "C" compiler must be told to use PASCAL parameter passing conventions.
This is accomplished as follows:
int pascal MyFunction(int x)
{
/* function body goes here */
}
That does a number of things:
1. The function arguments are pushed onto the stack in the proper
order as per BASIC, PASCAL conventions. Also, stack cleanup is
handled properly as per the same conventions.
2. It tells the C compiler to convert everything to upper case. That
includes variable and function names.
3. By default, Borland C adds an underscore character "_" in front of
all global identifiers (PUBLIC functions and and variables). BASIC
and Pascal (among others) don't. So, the "pascal" keyword above
takes care of that too. It may also be necessary to go into the
Options/Compiler/Advanced Code Generation dialog box to turn that
option off (depending on the version of the compiler you have).
For the command line compiler, the switch is "-u" if you need it.
2. Memory model
---------------
The "C" code must be compiled using the LARGE memory model (because
PowerBASIC uses FAR (32-bit) pointers exclusively.
In the Borland IDE, this is set in the Options/Compiler/Code Generation
dialog box.
If you're using the command-line compiler (BCC or TCC), the switch to use
on the command line is "-ml" (without the quotes).
3. Segment names
----------------
In PowerBASIC, the code from units and libraries whose code segments are
*NOT* explicitly named is combined into a segment named CODE. To allow
more than 64k of code from multiple units or libraries to be linked into
your main program, put a $CODE SEG metastatement in your UNIT source
ode,
using a name other than CODE. This is only necessary if you start
etting
an error message saying that the 64k segment limit has been exceeded.
Another reason you may want to use the $CODE SEG metastatement in your
unit source is that PowerBASIC only allows up to 16 segments to be
linked into your main program. When you compile a C module using the
LARGE memory model, each module is automatically given its own code and
data segment, named after the module being compiled (i.e. mymod.c would
generate mymod_TEXT and mymod_DATA for the code and data segments
respectively). If you're linking in more than a few C modules, you may
exceed the PowerBASIC 16 segment limit.
In the Borland IDE, you specify the names for the code and data segments
in the Options/Compiler/Names dialog box. The defaults are "*" for all
the segments, which has the effect of naming the segments after the
ource
modules with a _TEXT or _DATA suffix (i.e. mymod.c would generate
mymod_TEXT and mymod_DATA). Just TAB over to the Code Segment field and
type in CODE, then TAB over to the Data Segment field and type in DATA.
If you're using the command-line compiler (BCC or TCC), the switches to
use are:
-zC for the code segment (i.e. zCMyCode) and
-zR for the data segment (i.e. zRdata)
So a complete command line would look something like this:
BCC -c -ml -zCcode -zRdata mymod.c
This would generate "mymod.obj" compiled with the large memory model,
with the correct segment names, ready to be linked into you PowerBASIC
program.
(Continued to next message)
---
* QMPro 1.53 * VaporHelp - Verb; {call MicroSoft Tech Support}
--- WILDMAIL!/WC v4.12
---------------
* Origin: Toast House * (314) 994-0312 * (1:100/560.0)
|