Calling C Functions via FFI
Invoke libc and custom shared library functions from Lua with zero overhead.
Direct C Function Calls
Once declared with ffi.cdef, C functions are called exactly like Lua functions through the library object. Arguments and return values are automatically converted.
local ffi = require("ffi")
ffi.cdef[[ double sin(double x); ]]
local m = ffi.load("m")
print(m.sin(math.pi / 2)) -- 1.0Type Conversion
Lua numbers become C doubles by default. The FFI converts to the declared C parameter type automatically. Booleans become 0/1. Strings become const char*.
All lessons in this course
- FFI Basics: ffi.cdef and ffi.load
- Calling C Functions via FFI
- Pointers, Structs, and Arrays in FFI
- Callbacks: Passing Lua Functions to C