next up previous contents index search.gif
Next: 8.7 Assembler functions Up: 8. Using functions and Previous: 8.5 forward defined functions


8.6 External functions

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.

External directive

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{external\ di...
...ndex} \synt{integer\ constant}
\end{displaymath}\end{displaymath}\end{syntdiag}
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. If the external modifier is followed by a string constant:

external 'lname';
Then this tells the compiler that the function resides in library 'lname'. The compiler will the automatically link this library to your program.

You can also specify the name that the function has in the library:


external 'lname' name Fname;
This 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. Under WINDOWS and OS/2, you can also use the following form:

external 'lname' Index Ind;
This tells the compiler that the function resides in library 'lname', but with index Ind. The compiler will the automatically link this library to your program, and use the correct index for the function.

root
1999-06-10