// libcall.rg — calling external C functions at link time (@libcall) // // `@libcall("symbol", .{args...})` binds a symbol against libc or any // library named with `-l`/`-L`, resolved by the system linker into a native // executable built with `rinne build --native`. It's available ONLY in the // linked native build (the freestanding `--no-libc` path has no linker). // Up to six integer args; the returned `int` lands in `rax` and can feed // `return`. // // Build (links libc implicitly — `abs` lives there): // rinne build --native examples/libcall.rg // ./examples/libcall.rgx && echo "exit=$?" # -> 7 // // To call a function from YOUR own C library, compile one first, e.g. // cat > /tmp/triple.c <<'EOF' // int triple(int x) { return x * 3; } // EOF // cc -shared -fPIC -o /tmp/libtriple.so /tmp/triple.c // rinne build --native examples/libcall.rg -L /tmp -ltriple // // (uncomment the triple line below and the immutables above main; uncomment // nothing else — but note only ONE return runs. Swap the active return.) fn main() int { // libc `abs`: pass one integer arg (-7), get the int result back (7). return @libcall("abs", .{-7}); } // For a function from your own library (the -ltriple build above), uncomment // the main below INSTEAD — a script has exactly one `main`: // fn main() int { // return @libcall("triple", .{7}); // -> 21 // }