next up previous contents
Next: Making libraries Up: Linking issues Previous: Linking to an object

Linking to a library

 

To link your program to a library, the procedure depends on how you declared the external procedure.

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. On WINDOWSNT it will link to Name.dll, on LINUX your program will be linked to library libname, which can be a static or dynamic library.

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 WINDOWS 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 :
ppc386  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
Thu Sep 10 14:04:11 CEST 1998