C FFI
C FFI
Thagore provides direct access to C functions through extern func declarations. Any function available in a C shared library or the system’s C standard library can be called.
Basic C FFI
extern func strlen(s: str) -> i32extern func puts(text: str) -> i32
func main() -> i32: let msg = "Hello from FFI!" puts(msg) return 0Math library FFI
Call C math functions directly:
extern func sqrt(x: f64) -> f64extern func pow(base: f64, exp: f64) -> f64extern func fabs(x: f64) -> f64
func calc_hypotenuse(a: f64, b: f64) -> f64: return sqrt(a * a + b * b)
func main() -> i32: return 0Memory management FFI
extern func malloc(size: i32) -> ptrextern func free(p: ptr) -> voidextern func memcpy(dst: ptr, src: ptr, size: i32) -> ptr
func main() -> i32: let buf = malloc(1024) free(buf) return 0FFI type mapping
| Thagore type | C equivalent |
|---|---|
i32 | int / int32_t |
i64 | int64_t |
f64 | double |
str | const char* |
ptr | void* |
void | void |
There is no f32 type available to user code. Use f64 for floating-point FFI, or declare the extern with f64 and accept the implicit widening.
The symbol name in extern func must exactly match the C symbol resolved at link time.
Linking external libraries
Pass link flags to thagc build:
thagc build app.tg -o app \ --link-dir=/opt/vendor/lib \ --link-lib=vendor \ --link-arg=-Wl,-rpath,/opt/vendor/libWhat is not in the current compiler
| Feature | Status |
|---|---|
Python bridge (__thg_py_*) | not implemented |
f32 type | not available to user code |
| variadic C functions | limited support |