Every operating system comes with a "C standard library" or, simply, libc. It contains the fundamental building blocks of C programming: memory allocation, file I/O, string manipulation, system calls, and much more.

Affix provides a shortcut to access this library without you needing to know its exact filename (which varies wildly between Linux, macOS, and Windows).

The Recipe

We will bind the standard C function puts which takes a string, prints it to stdout, and appends a newline character.

use v5.40;
use Affix;

# 1. Bind
# libc is a helper function exported by Affix.
# It returns the handle to the system's standard library.
affix libc, 'puts' => [String] => Int;

# 2. Call
# Note: puts() automatically adds a newline "\n"
puts('Hello World');

# 3. Check Result
# puts returns a non-negative integer on success, or EOF (-1) on error.
my $ret = puts('Affix makes FFI easy.');
say 'Successfully wrote to stdout.' if $ret >= 0;

How It Works

Kitchen Reminders