LEVIATHAN vd52e79c4 · d52e79c486

Standard Library · ffi

class Library

A dynamically loaded library, for looking up raw addresses in it.

since 0.1.0-alpha.1linuxwindows

Overview

Foreign functions declared with @extern never need this class; it is for handing a function or data address from one C library to another. WebAssembly has no dynamically loaded libraries.

Examples

Looking up a symbol

uses ffi;
use io::console;

void main() {
    Library lib = Library::open("libc.so.6");
    console.writeln(lib.sym("strlen").isNull());
    lib.close();
}
main();
false

Constructors

open

Library::open(string path)

Loads a library from a file.

Parameters

path
The library file.

Throws

ffi.FfiException
when the library cannot be loaded.

Examples

Opening a library

uses ffi;
use io::console;

void main() {
    Library lib = Library::open("libc.so.6");
    console.writeln(lib.live);
    lib.close();
}
main();
true

Fields

h

int h

The runtime's handle of the library.

live

bool live

Whether the library is still open.

Methods

close

close() -> void

Unloads the library. Calling it again does nothing.

Examples

Closing twice

uses ffi;
use io::console;

void main() {
    Library lib = Library::open("libc.so.6");
    lib.close();
    lib.close();
    console.writeln(lib.live);
}
main();
false

sym

sym(string name) -> Ptr

Looks up the address of a symbol.

Parameters

name
The symbol.

Returns

Its address.

Throws

ffi.FfiException
when the library is closed or has no such symbol.

Examples

Found and missing symbols

uses ffi;
use io::console;

void main() {
    Library lib = Library::open("libc.so.6");
    console.writeln(lib.sym("abs").isNull());
    try {
        lib.sym("no_such_symbol_in_libc");
    } catch (FfiException e) {
        console.writeln(e.message);
    }
    lib.close();
}
main();
false
symbol 'no_such_symbol_in_libc' not found