SultanC
  1. Home
  2. Examples
  3. FFI

Calling a C library function through `extern`; the call needs `unsafe`

ffi/extern_call.sn
// Calling a C library function through `extern`; the call needs `unsafe`.
// Build with the C library linked: sultanc extern_call.sn -l c
// Or interpret it: sultanc interpret extern_call.sn finds labs at run time
// (docs/en/internals/interpreter.md, "Dynamic C functions").
import std.io.println;
import std.text.number;

extern def labs(value: i64): i64;

def main(): int {
    let distance: mutable i64 = 0;
    unsafe {
        distance := labs(-1234);
    }
    println(int_to_text(distance as int));
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/ffi/extern_call.sn

Or compile it to an executable:

./build/sultanc examples/en/ffi/extern_call.sn -o build/example && ./build/example

Source on GitHub   All examples