Linking Libraries

The #Link attribute binds an extern declaration to a native library (.dll, .so, .dylib). The operating-system loader resolves the library by name at run time.

#Link("libm.so")
extern func sqrt(x: float64) -> float64;

#Link("libm.so")
extern func pow(base: float64, exp: float64) -> float64;

A single #Link may also annotate a whole extern block, applying the library to every function inside it.

Example: Printing with the Windows API

#Link("Kernel32.dll")
extern func GetStdHandle(handle: uint32) -> *opaque;

#Link("Kernel32.dll")
extern func WriteFile(
    handle: *opaque,
    buffer: *opaque,
    numberOfBytesToWrite: uint32,
    numberOfBytesWritten: *var uint32,
    overlapped: *Overlapped,
) -> bool32;

func Main() -> int {
    let text = "Hello, Rux!\n";
    var written: uint32 = 0;
    let stdout = GetStdHandle(-11 as uint32); // STD_OUTPUT_HANDLE
    WriteFile(stdout, text.data, text.length as uint32, @written, null);
    return 0;
}

Per-Platform Libraries

Library names differ across systems, so guard the declarations with conditional compilation rather than a platform attribute:

import Core::{ #target };

when #target.os == .Windows {
    #Link("Kernel32.dll")
    extern func GetTickCount64() -> uint64;
} else when #target.os == .Linux {
    #Link("librt.so")
    extern func clock_gettime(clockid: int32, tp: *opaque) -> int32;
}

See #Link for the full attribute reference, including binding a differently named exported symbol.

See Also