The string of an extern fn is the C function's prototype, and each Auk parameter and the
result must fit the C type in the same position: C integers are int, bool is bool,
float/double are float, const char * is str (a result may be str?: NULL is none),
other pointers are ffi.Ptr or an extern type, a list [T] is T *, a var parameter is an
out-parameter (int *, char **), and void is (). A "T (*)(params)" prototype calls a
function pointer, which comes first as an ffi.Ptr, and "(T)" casts its one argument, an ffi.Ptr or
an extern type, to the pointer type T; "#embed path" is a file read at compile
time, returned as [int], str, ffi.Ptr or int (its length), with no parameters. Variadic functions, structs by value and
callbacks from C into Auk aren't supported.
Wrong#
[package]
name = "app"
version = "0.1.0"
effects = ["ffi"]extern fn strlen(s: str): str = "size_t strlen(const char *)"
fn main() uses ffi { print(strlen("hello")) }Fixed#
extern fn strlen(s: str): int = "size_t strlen(const char *)"
fn main() uses ffi { print(strlen("hello")) }Common causes#
- An Auk type that can't hold the C value (a
size_tdeclaredstr). - A C typedef the FFI doesn't know: write its underlying type, or
extern type X = "X". - A prototype with a different number of parameters than the Auk declaration.