Skip to content

libc

libc is a binding layer that exposes the C standard library, POSIX and Linux system calls to Mojo through external_call.

Every header gets its own sub-package, so you import only what you need:

libc.math      libc.string     libc.stdio      libc.stdlib
libc.unistd    libc.dirent     libc.time       libc.signal
libc.pthread   libc.network    libc.sys.*      ...

Status

The package is developed against Mojo 1.1.0 and is published to the mojo-force channel on prefix.dev. It is exercised on Linux (x86-64) and macOS (Apple silicon).

Quick start

from libc.math import sqrt
from libc.string import strlen
from libc.utils import string_to_char_ptr


def main() raises:
    print(sqrt(2.0))

    var ptr = string_to_char_ptr("hello")
    print(strlen(ptr))
    ptr.unsafe_free()

Memory and environment come from libc.stdlib:

from libc.stdlib import atoi, free, malloc
from libc.utils import string_to_char_ptr


def main():
    var block = malloc(8)
    block.unsafe_store(0, 65)
    _ = free(block)

    var digits = string_to_char_ptr("512")
    print(atoi(digits))
    digits.unsafe_free()

See Install and Usage for the details.

License

MIT. See LICENSE.