Quick start manual

Procedures and functions
6-7
Declaring procedures and functions
When importing a C function that takes a variable number of parameters, use the
varargs directive. For example,
function printf(Format: PChar): Integer; cdecl; varargs;
The varargs directive works only with external routines and only with the cdecl
calling convention.
Linking to object files
To call routines from a separately compiled object file, first link the object file to your
application using the $L (or $LINK) compiler directive. For example,
On Windows:
{$L BLOCK.OBJ}
On Linux:
{$L block.o}
links BLOCK.OBJ (Windows) or block.o (Linux) into the program or unit in which it
occurs. Next, declare the functions and procedures that you want to call:
procedure MoveWord(var Source, Dest; Count: Integer); external;
procedure FillWord(var Dest; Data: Integer; Count: Integer); external;
Now you can call the MoveWord and FillWord routines from BLOCK.OBJ (Windows)
or block.o (Linux).
Declarations like the ones above are frequently used to access external routines
written in assembly language. You can also place assembly-language routines
directly in your Delphi source code; for more information, see Chapter 13, “Inline
assembly code”.
Importing functions from libraries
To import routines from a dynamically loadable library (.so or .DLL), attach a
directive of the form
external stringConstant;
to the end of a normal procedure or function header, where stringConstant is the
name of the library file in single quotation marks. For example, on Windows
function SomeFunction(S: string): string; external 'strlib.dll';
imports a function called SomeFunction from strlib.dll.
On Linux,
function SomeFunction(S: string): string; external 'strlib.so';
imports a function called SomeFunction from strlib.so.
You can import a routine under a different name from the one it has in the library. If
you do this, specify the original name in the external directive:
external stringConstant
1
name stringConstant
2
;