ZetCode

Pascal C interoperability

last modified November 19, 2026 Free Pascal 3.2.2

C libraries are the common language of system programming on Unix: libc, OpenSSL, SQLite, and thousands of smaller libraries expose a C interface. Free Pascal can call them directly. A routine is declared with the external directive and a calling convention, structures are declared as records with matching layout, and strings cross the boundary as PChar. This article calls libc, links a C object file, passes a record, uses a C callback, and builds a Pascal shared library for a C program.

Calling a C library function

The external directive tells the compiler that the body of a routine is not in the Pascal source. The optional library name selects the shared object, and name gives the symbol as the C compiler emitted it. cdecl selects the C calling convention, and the ctypes unit provides the C type aliases.

libcstr.pas
program libcstr;
{$mode objfpc}{$H+}

uses
  ctypes, SysUtils;

function CStrLen(S: PChar): SizeInt; cdecl; external 'c' name 'strlen';

var
  S: string;

begin
  S := 'Pascal calls C';
  WriteLn('length: ', CStrLen(PChar(S)));
end.

external 'c' links the C library, and name 'strlen' resolves the symbol; without the name clause the compiler would look for a symbol called CStrLen. The cast PChar(S) passes a pointer to the string's bytes, which is exactly what strlen expects. The function returns SizeInt, the Pascal equivalent of size_t; using Integer would work on this platform but would be wrong on a 32-bit build.

$ fpc -gl libcstr.pas
$ ./libcstr
length: 14

Linking a C object file

A function that is not in a shared library can be compiled to an object file and linked into the Pascal program. The {$L} directive names the object file, and the Pascal declaration repeats the C signature. The C file below is compiled with gcc -c, which produces add.o without linking an executable.

add.c
int add(int a, int b)
{
    return a + b;
}

The Pascal declaration uses cint for both parameters and the result, and omits the library name because the object file is linked directly. The external name is the same as the C name, so no name clause is needed.

adddemo.pas
program adddemo;
{$mode objfpc}{$H+}
{$L add.o}

uses
  ctypes;

function add(a, b: cint): cint; cdecl; external;

begin
  WriteLn('add(20, 22) = ', add(20, 22));
end.

The order matters: gcc must create add.o before fpc compiles the program. The object file is linked into the executable, so the program does not need a shared library at run time.

$ gcc -c add.c -o add.o
$ fpc -gl adddemo.pas
$ ./adddemo
add(20, 22) = 42

Passing a structure

A C structure maps to a Pascal record with the same fields in the same order. Both compilers align the fields naturally, so two int fields produce an eight-byte record on x86-64. The C function takes a pointer to the structure; on the Pascal side the parameter is a pointer to the record.

point.c
typedef struct {
    int x;
    int y;
} Point;

int point_sum(Point *p)
{
    return p->x + p->y;
}

The Pascal record uses cint rather than Integer, which documents the C type and stays correct if the Pascal integer size changes. A named pointer type is required in the function declaration: PPoint = ^TPoint. ObjFPC does not accept ^TPoint directly as a parameter type.

pointdemo.pas
program pointdemo;
{$mode objfpc}{$H+}
{$L point.o}

uses
  ctypes;

type
  PPoint = ^TPoint;
  TPoint = record
    X, Y: cint;
  end;

function point_sum(P: PPoint): cint; cdecl; external;

var
  P: TPoint;

begin
  P.X := 19;
  P.Y := 23;
  WriteLn('size: ', SizeOf(TPoint));
  WriteLn('sum:  ', point_sum(@P));
end.

SizeOf(TPoint) is 8, which matches the C compiler's sizeof(Point). For a structure with padding, use the same field types and order on both sides, and check the size before trusting the layout. A packed Pascal record does not necessarily match a packed C structure; the safe rule is to mirror the C declaration exactly.

$ gcc -c point.c -o point.o
$ fpc -gl pointdemo.pas
$ ./pointdemo
size: 8
sum:  42

Callbacks

A C function can call back into Pascal through a procedural type. The Pascal type must use cdecl and the parameters must match the C callback signature. The address of a Pascal routine is passed with @.

callback.c
typedef int (*binop)(int, int);

int apply(binop f, int a, int b)
{
    return f(a, b);
}

The C type binop is a pointer to a function taking two ints and returning one. The Pascal type TBinOp is declared with the same signature and cdecl, and Multiply is an ordinary Pascal function that happens to follow the C convention.

callbackdemo.pas
program callbackdemo;
{$mode objfpc}{$H+}
{$L callback.o}

uses
  ctypes;

type
  TBinOp = function(a, b: cint): cint; cdecl;

function apply(f: TBinOp; a, b: cint): cint; cdecl; external;

function Multiply(a, b: cint): cint; cdecl;
begin
  Result := a * b;
end;

begin
  WriteLn('apply(@Multiply, 6, 7) = ', apply(@Multiply, 6, 7));
end.

The callback runs while apply is on the stack, in the same thread. Any global state that Multiply touches is therefore shared with the C code, and the callback must not raise a Pascal exception through C frames: catch it inside the callback and return an error value instead.

$ gcc -c callback.c -o callback.o
$ fpc -gl callbackdemo.pas
$ ./callbackdemo
apply(@Multiply, 6, 7) = 42

Calling Pascal from C

The traffic can go the other way. A Pascal library is compiled to a shared object, and the routines listed in its exports clause are visible to a C program. The Pascal functions must use cdecl and types that C understands.

mathlib.pas
library mathlib;
{$mode objfpc}{$H+}

function Add(a, b: Integer): Integer; cdecl;
begin
  Result := a + b;
end;

function Multiply(a, b: Integer): Integer; cdecl;
begin
  Result := a * b;
end;
exports
  Add, Multiply;

begin
end.

fpc -gl mathlib.pas creates libmathlib.so on Linux. The C program declares the two functions and links the library with -lmathlib. The rpath entry lets the executable find the library in its own directory instead of relying on a system-wide install.

usemath.c
#include <stdio.h>

int Add(int a, int b);
int Multiply(int a, int b);

int main(void)
{
    printf("Add(20, 22) = %d\n", Add(20, 22));
    printf("Multiply(6, 7) = %d\n", Multiply(6, 7));
    return 0;
}

The C compiler needs only the declarations; the linker resolves them against the shared object. On Windows the library would be a DLL and the import library would take the place of -lmathlib, but the Pascal source and the exported names stay the same.

$ fpc -gl mathlib.pas
$ gcc usemath.c -L. -lmathlib -Wl,-rpath,'$ORIGIN' -o usemath
$ ./usemath
Add(20, 22) = 42
Multiply(6, 7) = 42

Types and calling conventions

Use the ctypes aliases for every value that crosses the boundary: cint, cuint, clong, csize_t, cdouble, and cfloat. A C char * becomes PChar for a NUL-terminated string; a buffer becomes PChar plus a length, or PByte plus a byte count. Pascal string and dynamic arrays are managed and must not be passed where C expects a raw pointer.

cdecl is the default on most non-Windows platforms, but writing it explicitly documents the interface and keeps the code correct on Windows. stdcall belongs to the Windows API, and external without a library name expects the symbol in the final link, as in the object file examples. Name mangling is not an issue for plain C functions, but C++ libraries export decorated names and usually need a C wrapper.

Source

Free Pascal: external procedures

Free Pascal: ctypes unit

Foreign function interface on Wikipedia

In this article we called libc with external, linked a C object file, passed a structure by pointer, used a C callback, and exported Pascal functions to C. The pointers article explains the address and dereference operators used at the boundary, and the compiling and debugging article shows how to inspect the resulting binary.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pascal tutorials.