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
#Link— the full attribute referenceexternDeclarations — the declarations this attribute links- Conditional Compilation — selecting a library per platform
Extern Declarations
The extern keyword declares functions that are defined outside Rux — typically in C / C++ libraries.
Overview
Some of a Rux program is decided while it is being compiled, before any of it runs. The compiler exposes facts about the build, selects which code to compile, and substitutes values that are fixed the moment the program is built. Together these features let one source tree target several platforms and configurations without a preprocessor or a separate build language.