Link
Names the native library an extern function is imported from, and optionally the exported symbol to bind to. The operating system loader resolves the library by name at run time.
Syntax
#Link("Kernel32.dll")
extern func Beep(freq: uint32, duration: uint32) -> bool32;
The library is a plain name — vulkan.so, vulkan.dylib, vulkan.dll — without a path. The argument may be a string literal or a compile-time string constant.
Binding a Different Symbol Name
By default the exported symbol matches the Rux declaration name. A second argument binds to a differently named export, so the Rux name can follow local conventions:
#Link("libm.so", "sqrt")
extern func SquareRoot(x: float64) -> float64; // calls C's sqrt
Applying to an extern Block
The one-argument form may annotate an entire extern block, applying the library to every function inside it:
#Link("Kernel32.dll")
extern {
func GetStdHandle(handle: uint32) -> *opaque;
func WriteFile(
handle: *opaque,
buffer: *opaque,
numberOfBytesToWrite: uint32,
numberOfBytesWritten: *var uint32,
overlapped: *Overlapped,
) -> bool32;
}
Compatibility Spellings
#Library("name") and #Symbol("name") set the library and symbol separately. They are retained for compatibility and cannot be combined with #Link — prefer #Link, which expresses both in one attribute.
Selecting a Library per Platform
Because a library name differs across systems, guard the declarations with conditional compilation:
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 Also
externDeclarations — the declarations this attribute links- Foreign Function Interface — the full FFI workflow
- Conditional Compilation — choosing a library per platform
Overview
An attribute attaches metadata to a declaration, written as an #Name(...) attribute call on the line before the item it annotates. Attributes instruct the compiler — to link a foreign library, fix a calling convention, or flag a function at its call sites — without changing the declaration's own code.
Abi
Selects the calling convention a function is compiled with — the contract for how arguments, the return value, and the stack are arranged. Most code never needs it: Rux uses the right convention for the target automatically. Reach for #Abi at a foreign boundary that must follow a specific ABI regardless of the platform default.