Board index » delphi » Linking (turbo) C in Borland pascal 7.0

Linking (turbo) C in Borland pascal 7.0

Hi

My question seems like a FAQ to me but I haven't been able to get a
complete and accurate answer. It's not covered very well in Timo
Salmi's faq and also not in the Turbo pascal 6.0 proffessional manuals
that I have. The manuals describe how to link in TASM .obj files, but
tell little about C .obj files.

Anyway, this is what I'd like to do:

let's say I have a unit

Unit TestLink;

interface

type
  block = array[0..255] of word;
  blockarray = array[0..3] of block;
  blockarrayptr = ^blockarray;

var
  P : BlockArrayPtr;

function FillBlocks : blockarrayptr;
{$L CRoutine.obj}

implementation

begin
  P := FillBlock;
end.

The external routine in C should allocate the memory and I should be
able to reference it (through the use of P^[0][231] etc. etc.)
Letting pascal allocate the mem and just have it filled is also an
option, I don't care.

My question is this: how should I do this and what do I have to tell
the guy with the c-source. What does he need to do to make a valid
pascal linkable .obj file?

Marnix

- - - - Marnix Timmermans - - - -
-   m.p.h.l.timmerm...@kub.nl   -
- http://huizen.dds.nl/~checkhd -

 

Re:Linking (turbo) C in Borland pascal 7.0


In article <321224a3.5225389@news>,

Quote
Marnix Timmermans <m.p.h.l.timmerm...@kub.nl> wrote:
>Hi

>My question seems like a FAQ to me but I haven't been able to get a
>complete and accurate answer. It's not covered very well in Timo
>Salmi's faq and also not in the Turbo pascal 6.0 proffessional manuals
>that I have. The manuals describe how to link in TASM .obj files, but
>tell little about C .obj files.

>Anyway, this is what I'd like to do:

>let's say I have a unit

>Unit TestLink;

>interface

>type
>  block = array[0..255] of word;
>  blockarray = array[0..3] of block;
>  blockarrayptr = ^blockarray;

>var
>  P : BlockArrayPtr;

>function FillBlocks : blockarrayptr;
>{$L CRoutine.obj}

>implementation

>begin
>  P := FillBlock;
>end.

>The external routine in C should allocate the memory and I should be
>able to reference it (through the use of P^[0][231] etc. etc.)
>Letting pascal allocate the mem and just have it filled is also an
>option, I don't care.

First rule: do not use any library routines in C. That means memory
allocation is out. Use Pascal for that. In C define the functions as
type pascal. In pascal you need to define the same procedure as
external. If you need to call pascal routines from C, then remember that
the C compilers generally put much stricter rules on what registers can
be altered than turbo pascal does.  I do not remember C very well to
know how you can make pascal variables visible there. If you allocate
global variables in C, they cannot be initialized.

Why would you want to do that? Apart form C-compilers generally
producing more optimized code I do not see any reason for it.

Quote

>My question is this: how should I do this and what do I have to tell
>the guy with the c-source. What does he need to do to make a valid
>pascal linkable .obj file?

Osmo

Re:Linking (turbo) C in Borland pascal 7.0


Quote
m.p.h.l.timmerm...@kub.nl (Marnix Timmermans) wrote:
>Hi
>My question seems like a FAQ to me but I haven't been able to get a
>complete and accurate answer. It's not covered very well in Timo
>Salmi's faq and also not in the Turbo pascal 6.0 proffessional manuals
>that I have. The manuals describe how to link in TASM .obj files, but
>tell little about C .obj files.

There are some threads on alt.comp.lang.borland-delphi about this,
"Passing Delphi 1 vriable to C function via .obj module" et al.

There are a lot of restrictions on what TP will accept in an .obj
file, which puts a lot of restrictions on C code. Basically if the C
code is Turbo/Borland C and is an int pascal function which uses far
pointers and does not use *any* runtime routines, then you should be
OK.

Quote
>Anyway, this is what I'd like to do:
>let's say I have a unit
>Unit TestLink;
>interface
>type
>  block = array[0..255] of word;
>  blockarray = array[0..3] of block;
>  blockarrayptr = ^blockarray;
>var
>  P : BlockArrayPtr;
>function FillBlocks : blockarrayptr;

add external; .

Quote
>{$L CRoutine.obj}
>implementation
>begin
>  P := FillBlock;
>end.
>The external routine in C should allocate the memory

This is very unwise as the heap manager is probably not compatible.
Better to alloc in Pascal.

Quote
> and I should be
>able to reference it (through the use of P^[0][231] etc. etc.)

I assume there is a very good reason for it being 2 dimensional. I
don't know if C accesses 2-d arrays the same as Pascal (column -major
versus row-major) so this may not work.

Quote
>Letting pascal allocate the mem and just have it filled is also an
>option, I don't care.
>My question is this: how should I do this and what do I have to tell
>the guy with the c-source. What does he need to do to make a valid
>pascal linkable .obj file?

Use a Borland C compiler, medium model, declare functions as far
pascal. Do not use any run-time library functions. Pass pointers as
far.

Personally I would tell the c guy to write in Pascal or convert the
source to Pascal.

Regards,

--
Bob Cousins, Software Engineer.
http://www.demon.co.uk/sirius-{*word*104}netics/

Re:Linking (turbo) C in Borland pascal 7.0


[some useful comments deleted]

Quote
>Use a Borland C compiler, medium model, declare functions as far
>pascal. Do not use any run-time library functions. Pass pointers as
>far.

thanks, I'll ask him if there's run-time library functions in his
code.

Quote
>Personally I would tell the c guy to write in Pascal or convert the
>source to Pascal.

I can allways ask him to write the info to a seperate diskfile and
read the file in pascal. This wouldn't be a major problem in my case
(only a bit unelegant I guess)

Marnix

- - - - Marnix Timmermans - - - -
-   m.p.h.l.timmerm...@kub.nl   -
- http://huizen.dds.nl/~checkhd -

Other Threads