next up previous contents
Next: Making a shared library Up: Linking issues Previous: Explicitly linking an object

Linking your program to a library

  To link your program to a library, the procedure depends on how you declared the external procedure. If you used thediffers a little from the procedure when you link in an object file. although the declaration step remains the same (see 4.1 on how to do that).

In case you used the follwing syntax to declare your procedure:

Procedure ProcName (Args : TPRocArgs); external 'Name';
You don't need to take additional steps to link your file in, the compiler will do all that is needed for you.

In case you used

Procedure ProcName (Args : TPRocArgs); external;
You still need to explicity link to the library. This can be done in 2 ways:
  1. You can tell the compiler in the source file what library to link to using the {$LinkLib 'Name'} directive:
    {$LinkLib 'gpm'}
    This will link to the gpm library. On LINUX systems, you needn't specify the extension or 'lib' prefix of the library. The compiler takes care of that. On DOS or systems, you need to specify the full name.
  2. You can also tell the compiler on the command-line to link in a library: The -k option can be used for that. For example
    ppc386 -k'-lgpm' myprog.pp
    Is equivalent to the above method, and tells the linker to link to the gpm library.

As an example; consider the following program :

program printlength;

{$linklib c} { Case sensitive }

{ Declaration for the standard C function strlen }
Function strlen (P : pchar) : longint; cdecl;external;

begin
  Writeln (strlen('Programming is easy !'));
end.
This program can be compiled with :
pp  prlen.pp
Supposing, of course, that the program source resides in prlen.pp.

You cannot use procedures or functions that have a variable number of arguments in C. Pascal doesn't support this feature of C.



Michael Van Canneyt
Tue Mar 31 16:50:06 CEST 1998