Skip to main content

C Interop

Overview

One can write foreign function interfaces (FFIs) for C by preceding the function declaration with extern "C".

extern "C" fn my_ffi(...): type;

This prevents name mangling and forces the C Calling Convention.

Types like char are directly compatible with the C counterpart. The following C types are provided for writing FFIs:

TypeC counterpart
c_intint
c_uintunsigned int
c_lllong long
c_ullunsigned long long
c_llong
c_ulunsigned long
c_size_tsize_t
c_ssize_tssize_t
c_scharsigned char
c_ucharunsigned char
c_shortshort
c_ushortunsigned short
c_bool_Bool
c_floatfloat
c_doubledouble
c_ldoublelong double
c_intptr_tintptr_t
c_uintptr_tuintptr_t
c_ptrdiff_tptrdiff_t
c_intmax_tintmax_t
c_uintmax_tuintmax_t
c_wchar_twchar_t

Stay informed