Ada interfacing with C
last modified November 23, 2026
Most operating-system libraries are written in C, and Ada programs are
expected to call them without a wrapper layer. The language provides the
package Interfaces.C, whose types match the C types on the
platform, and two pragmas: Import declares an existing foreign
subprogram, and Export publishes an Ada subprogram under a C
name. The compiler and the linker do the rest. This article reads the sizes
of the C types, calls abs and strlen, compiles a C
file of its own, sorts through a C callback, and exports an Ada function back
to C.
The types that C uses
Interfaces.C declares char, int,
long, double, and the unsigned and modular types
such as size_t. They are not the same as the predefined Ada
types, because C leaves several of their sizes to the platform. The
'Size attribute reports the size in bits, so dividing it by eight
gives the number of bytes that the C compiler uses.
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C; use Interfaces.C;
procedure C_Sizes is
begin
Put_Line ("char =" & Integer'Image (char'Size / 8) & " bytes");
Put_Line ("int =" & Integer'Image (int'Size / 8) & " bytes");
Put_Line ("long =" & Integer'Image (long'Size / 8) & " bytes");
Put_Line ("double =" & Integer'Image (double'Size / 8) & " bytes");
Put_Line ("size_t =" & Integer'Image (size_t'Size / 8) & " bytes");
end C_Sizes;
On this 64-bit Linux machine int is four bytes, long
and double are eight, and size_t is an unsigned
64-bit type. The same program on a 32-bit target would print different
numbers, which is exactly why the package exists: the Ada declarations follow
the C compiler instead of guessing. size_t is modular, so it
cannot hold a negative value, while int is a signed integer
type.
$ gnatmake -gnat2022 -gnatwa c_sizes.adb $ ./c_sizes char = 1 bytes int = 4 bytes long = 8 bytes double = 8 bytes size_t = 8 bytes
Calling a C function
A foreign subprogram is declared like an Ada one, with the C parameter and
result types, and then bound to its C name by a pragma. The declaration has
no body. pragma Import (C, C_Abs, "abs") tells the compiler that
the body is the C function abs, and the linker resolves the call
against the C library that every GNAT program already uses.
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C; use Interfaces.C;
procedure C_Abs_Demo is
function C_Abs (N : int) return int;
pragma Import (C, C_Abs, "abs");
begin
Put_Line ("abs(-7) =" & int'Image (C_Abs (-7)));
end C_Abs_Demo;
The Ada name C_Abs and the C name abs need not be
the same; the third argument of the pragma supplies the external name. The
call passes an int and receives an int, so no
conversion is inserted. int'Image is available just like
Integer'Image, including its leading space for a non-negative
value, which is why the output has a single space after the equals sign.
$ gnatmake -gnat2022 -gnatwa c_abs_demo.adb $ ./c_abs_demo abs(-7) = 7
C strings and pointers
A C string is a pointer to characters that ends with a zero byte, not an Ada
String with a bound. The child package
Interfaces.C.Strings declares the access type
chars_ptr and the routines that convert between the two:
New_String allocates a C string from an Ada string,
Value copies it back, and Free releases the
allocation. The next program passes such a string to strlen.
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
procedure C_Strlen is
function C_Strlen (S : chars_ptr) return size_t;
pragma Import (C, C_Strlen, "strlen");
Text : chars_ptr := New_String ("interfacing");
begin
Put_Line (Value (Text) & " has" &
size_t'Image (C_Strlen (Text)) & " characters");
Free (Text);
end C_Strlen;
strlen returns a size_t, which is printed with its
own 'Image; there is no implicit conversion to
Integer. Text cannot be a constant because
Free takes it as an in out parameter: it releases
the storage and, on this implementation, sets the pointer to null. Forgetting
Free leaks the allocation, and passing a null
chars_ptr to strlen would crash the program.
$ gnatmake -gnat2022 -gnatwa c_strlen.adb $ ./c_strlen interfacing has 11 characters
A C file of our own
A program is not limited to the C library. A C source file is compiled to an
object file with the C compiler, and the object is handed to the Ada linker
after -largs. The next example converts Celsius to Fahrenheit in
C and formats the results in Ada, which shows how a double
crosses the boundary in both directions.
double fahrenheit(double celsius)
{
return celsius * 9.0 / 5.0 + 32.0;
}
The Ada declaration repeats the C signature with double for both
the parameter and the result. The body belongs to the C file, so the
declaration ends with the import pragma. The loop variable C is
an Integer from the predefined type, and it is converted
explicitly with double (C) before the call.
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C; use Interfaces.C;
procedure C_Temperature is
package Double_IO is new Ada.Text_IO.Float_IO (double);
function Fahrenheit (C : double) return double;
pragma Import (C, Fahrenheit, "fahrenheit");
begin
for C in 0 .. 100 loop
if C mod 20 = 0 then
Put ("C =" & Integer'Image (C) & " F =");
Double_IO.Put (Fahrenheit (double (C)),
Fore => 4, Aft => 1, Exp => 0);
New_Line;
end if;
end loop;
end C_Temperature;
The C function is compiled first, and its object file is passed to the link
step rather than to the compiler. gnatmake builds the Ada unit as
usual and then appends everything after -largs to the linker
command. The output is a small table in which the freezing point and the
boiling point of water appear at the two ends.
$ gcc -c celsius.c $ gnatmake -gnat2022 -gnatwa c_temperature.adb -largs celsius.o $ ./c_temperature C = 0 F = 32.0 C = 20 F = 68.0 C = 40 F = 104.0 C = 60 F = 140.0 C = 80 F = 176.0 C = 100 F = 212.0
Callbacks with qsort
C also calls back into Ada. The standard library's qsort takes a
comparison function as its last argument, and that function may be written in
Ada. Two rules make it work: the callback must have C convention, which is
requested with pragma Convention (C, Compare), and it must accept
the untyped addresses that C passes as void *. The package
System.Address_To_Access_Conversions turns such an address back
into a pointer to an integer.
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C; use Interfaces.C;
with System;
with System.Address_To_Access_Conversions;
procedure C_Qsort is
type Data_Array is array (1 .. 6) of aliased int;
Data : Data_Array := [5, 3, 9, 1, 7, 3];
package Int_Conversions is
new System.Address_To_Access_Conversions (int);
function Compare (Left, Right : System.Address) return int;
pragma Convention (C, Compare);
procedure C_Qsort (Base : System.Address;
Nmemb : size_t;
Size : size_t;
Compar : System.Address);
pragma Import (C, C_Qsort, "qsort");
function Compare (Left, Right : System.Address) return int is
L : constant Int_Conversions.Object_Pointer :=
Int_Conversions.To_Pointer (Left);
R : constant Int_Conversions.Object_Pointer :=
Int_Conversions.To_Pointer (Right);
begin
if L.all < R.all then
return -1;
elsif L.all > R.all then
return 1;
else
return 0;
end if;
end Compare;
begin
C_Qsort (Data (Data'First)'Address, size_t (Data'Length),
size_t (int'Size / 8), Compare'Address);
for Value of Data loop
Put (int'Image (Value));
end loop;
New_Line;
end C_Qsort;
The array elements are declared aliased so that their addresses
are stable while C works on them. Data (Data'First)'Address is
the address of the first element, Data'Length is the number of
elements, and int'Size / 8 is the size of one element in bytes.
The callback returns a negative number, zero, or a positive number according
to the order of the two values, as C requires. The two occurrences of 3 stay
next to each other, because qsort is not required to be stable
but this small array happens to preserve them.
$ gnatmake -gnat2022 -gnatwa c_qsort.adb $ ./c_qsort 1 3 3 5 7 9
Exporting an Ada subprogram
The direction can also be reversed: an Ada subprogram is published under a C
name, and C code calls it as if it were written in C. The subprogram must be
declared at library level, and its parameters and result must use convention
compatible types, so Interfaces.C.int is the natural choice. The
Export pragma sits in the package specification, next to the
declaration it applies to.
with Interfaces.C;
package Clamp is
function Clamp_Value (V, Low, High : Interfaces.C.int)
return Interfaces.C.int;
pragma Export (C, Clamp_Value, "ada_clamp");
end Clamp;
The body compares the value with the two bounds. The operators of
Interfaces.C.int are not directly visible through the
with clause, so the body adds use type
Interfaces.C.int; to bring them in. Without it, the compiler reports
that the operator for the type is not directly visible and suggests a
use clause.
package body Clamp is
use type Interfaces.C.int;
function Clamp_Value (V, Low, High : Interfaces.C.int)
return Interfaces.C.int is
begin
if V < Low then
return Low;
elsif V > High then
return High;
else
return V;
end if;
end Clamp_Value;
end Clamp;
Compiling the package produces an object file in which the function is
visible under the name ada_clamp, ready for any C translation
unit to declare and call. The next section writes that C translation unit and
links it with the Ada main program.
Calling the exported subprogram
The C wrapper declares the Ada function with extern and forwards
its own arguments to it. On the Ada side the main program declares the
wrapper as an imported function and calls it with values below, inside, and
above the range. The same program also calls the Ada function directly, which
shows that the export does not replace the normal Ada interface.
extern int ada_clamp(int v, int low, int high);
int clamp_range(int v, int low, int high)
{
return ada_clamp(v, low, high);
}
The wrapper is compiled with the C compiler, and the object is again passed
after -largs. The linker combines the C object with the Ada
objects and the GNAT runtime, and the resulting executable contains both
languages. The three calls exercise both directions of the interface in one
run.
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C; use Interfaces.C;
with Clamp;
procedure C_Export is
function Clamp_Range (V, Low, High : int) return int;
pragma Import (C, Clamp_Range, "clamp_range");
begin
Put_Line ("clamp(-5, 0, 100) =" &
int'Image (Clamp_Range (-5, 0, 100)));
Put_Line ("clamp(150, 0, 100) =" &
int'Image (Clamp_Range (150, 0, 100)));
Put_Line ("direct Ada call =" &
int'Image (Clamp.Clamp_Value (42, 0, 100)));
end C_Export;
The first two lines go through C and back into Ada, and both are clamped to the nearest bound. The last line bypasses C entirely and reaches the same function through the package. Nothing in the output reveals which path a value took, which is the point of a well-defined interface.
$ gcc -c clamp_wrap.c $ gnatmake -gnat2022 -gnatwa c_export.adb -largs clamp_wrap.o $ ./c_export clamp(-5, 0, 100) = 0 clamp(150, 0, 100) = 100 direct Ada call = 42
Next steps
The series continues with
GPR projects and gprbuild, which replaces the
growing gnatmake command lines with a project file that names the
sources, the switches, and the C object files. The
access types article explains the pointer
types used by chars_ptr, and
sequential and direct I/O shows
the binary layout of records, a useful background for foreign data.
Source
Ada Reference Manual: Interfacing to C
Ada Reference Manual: Interfacing Pragmas
In this article we have measured the C types, imported abs and
strlen, passed C strings, compiled a C source file, sorted
through a callback with qsort, and exported an Ada function that
C calls back. The access types article
explains the access and aliased declarations that
the callback example relies on.
Author
List all Ada tutorials.