0Pricing
Lua Academy · Lesson

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.0

Type 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

  1. FFI Basics: ffi.cdef and ffi.load
  2. Calling C Functions via FFI
  3. Pointers, Structs, and Arrays in FFI
  4. Callbacks: Passing Lua Functions to C
← Back to Lua Academy