next up previous contents index
Next: StdCall Up: Modifiers Previous: external

Export

Sometimes you must provide a callback function for a C library, or you want your routines to be callable from a C program. Since Free Pascal and C use different calling schemes for functions and proceduresgif, the compiler must be told to generate code that can be called from a C routine. This is where the Export modifier comes in. Contrary to the other modifiers, it must be specified separately, as follows:

function DoSquare (X : longint) : longint; export;

begin
...
end;
The square brackets around the modifier are not allowed in this case.

Remark: You cannot call an exported function from within Free Pascal programs. If you try to do so, the compiler will complain when compiling your source code.

If you do want to call an exported procedure from pascal, you must use a dummy function:

Procedure RealDoSomething;
begin
...
end;

Procedure DoSomething; export;

begin
  RealDoSomething;
end;
In this example, from your Free Pascal code, you can call the RealDoSomething procedure. If someone wants to link to your code from a C program, he can call the DoSomething procedure. Both calls will have the same effect.

Remark: as of version 0.9.8, Free Pascal supports the Delphi cdecl modifier. This modifier works in the same way as the export modifier.

More information about these modifiers can be found in the Programmer's guide in the section on the calling mechanism and the chapter on linking.



Michael Van Canneyt
Thu Sep 10 14:02:43 CEST 1998