Skip to content

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) -> i32
extern func puts(text: str) -> i32
func main() -> i32:
let msg = "Hello from FFI!"
puts(msg)
return 0

Math library FFI

Call C math functions directly:

extern func sqrt(x: f64) -> f64
extern func pow(base: f64, exp: f64) -> f64
extern func fabs(x: f64) -> f64
func calc_hypotenuse(a: f64, b: f64) -> f64:
return sqrt(a * a + b * b)
func main() -> i32:
return 0

Memory management FFI

extern func malloc(size: i32) -> ptr
extern func free(p: ptr) -> void
extern func memcpy(dst: ptr, src: ptr, size: i32) -> ptr
func main() -> i32:
let buf = malloc(1024)
free(buf)
return 0

FFI type mapping

Thagore typeC equivalent
i32int / int32_t
i64int64_t
f64double
strconst char*
ptrvoid*
voidvoid

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:

Terminal window
thagc build app.tg -o app \
--link-dir=/opt/vendor/lib \
--link-lib=vendor \
--link-arg=-Wl,-rpath,/opt/vendor/lib

What is not in the current compiler

FeatureStatus
Python bridge (__thg_py_*)not implemented
f32 typenot available to user code
variadic C functionslimited support