[fpc-pascal]linking pascal objects/units into C

Peter Vreman peter at freepascal.org
Mon Jun 18 20:52:30 CEST 2001


At 13:50 18-6-01, you wrote:
>Hi myself!
>
>On Mon, Jun 18, 2001 at 12:43:50PM +0200, Oliver Graf wrote:
> > We have a pretty old pascal project (delphi 1 on win95 -- already
> > ported) and we need to interface it with various script languages
> > (python, perl, etc.). So we need to link the pascal units to some
> > C interfacing code for those script languages.
> >
> > Has anyone done this before and can provide us with some hints how
> > to archive this (i.e. creating a C header for the unit, calling
> > conventions, argument passing)?
> >
> > Target system is linux.
>
>This now breaks down to: which fpc runtime libs do I need to link
>(basically) to a C programm which uses a pascal unit? I've added
>rtl/libsyslinux.a and rtl/libsysutils.a, but _haltproc and INITFINAL
>(used in libsyslinux.a) are still missing.
>
>example:
>
>compile unit with 'fpc test'
>
>try to compile C with 'gcc test.c test.o 
>/usr/lib/fpc/1.0.4/units/linux/rtl/libsysutils.a 
>/usr/lib/fpc/1.0.4/units/linux/rtl/libsyslinux.a -o doittest'
>
>Any hints?

You need to create a library. The library will include all the required units.

library test;

procedure doit(i : integer); cdecl; export;
begin
    writeln('Hello World!');
    writeln(i);
    writeln();
end; { doit }

exports
   doit;

end.


This will create a file libtest.so. Copy this file into /usr/lib or any 
other path searched by ld. Then run ldconfig to update the ld.cache file. 
And finally you can compile test.c and link the library with:

gcc -o test test.c -ltest


Peter





More information about the fpc-pascal mailing list