The external modifier can be used to declare a function that resides in an external object file. It allows you to use the function in your code, and at linking time, you must link the object file containing the implementation of the function or procedure.
It replaces, in effect, the function or procedure code block. As such, it can be present only in an implementation block of a unit, or in a program.
As an example:
program CmodDemo; {$Linklib c} Const P : Pchar = 'This is fun !'; Function strlen (P : Pchar) : Longint; cdecl; external; begin Writeln ('Length of (',p,') : ',strlen(p)) end.
Remark The parameters in our declaration of the external function should match exactly the ones in the declaration in the object file. Since C is case sensitive, this means also that the name of the function must be exactly the same.
The external modifier has also an extended syntax:
external 'lname';Tells the compiler that the function resides in library 'lname'. The compiler will the automatically link this library to your program.
external 'lname' name Fname;Tells the compiler that the function resides in library 'lname', but with name 'Fname'. The compiler will the automatically link this library to your program, and use the correct name for the function.
external 'lname' Index Ind;Tells the compiler that the function resides in library 'lname', but with indexname Ind. The compiler will the automatically link this library to your program, and use the correct index for the function.