0Pricing
Lua Academy · Lesson

FFI Basics: ffi.cdef and ffi.load

Declare C types and load shared libraries with LuaJIT's ffi module.

FFI Basics: ffi.cdef and ffi.load is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is the FFI?

LuaJIT's FFI (Foreign Function Interface) lets you call C library functions and use C data structures directly from Lua, with no C wrapper code needed.

Requiring the FFI

Load the FFI library with require("ffi"). It is built into LuaJIT and not available in standard PUC Lua.

local ffi = require("ffi")

ffi.cdef: Declaring C Types

ffi.cdef(cDeclarations) parses C declarations and makes the types and function signatures available to the FFI. No actual code is compiled — just type info.

ffi.cdef[[
  int printf(const char *fmt, ...);
  double sqrt(double x);
  typedef struct { int x; int y; } Point;
]]

ffi.load: Loading a Shared Library

ffi.load(libname) loads a shared library and returns an object whose fields are the declared C functions.

local C = ffi.C       -- access to libc on POSIX
local m = ffi.load("m")  -- libm
print(m.sqrt(2.0))       -- 1.414...

Accessing libc Functions Directly

On POSIX systems, ffi.C provides access to all standard C library functions without an explicit load.

ffi.cdef[[ int puts(const char *s); ]]
ffi.C.puts("Hello from C!")

ffi.load Path

Pass the full path or a standard library name. On Linux use "m" for libm.so, "pthread" for pthreads, etc.

Declaring Structs

Declare C structs in cdef, then allocate and access them from Lua.

ffi.cdef[[
  typedef struct { float x, y, z; } Vec3;
]]
local v = ffi.new("Vec3", {1, 2, 3})
print(v.x, v.y, v.z)  -- 1.0  2.0  3.0

Type Annotations and Qualifiers

cdef supports const, unsigned, int32_t, uint64_t and other standard C types.

Multiple cdef Calls

You can call ffi.cdef multiple times. Duplicate declarations are ignored. Typically organize by library.

Platform Differences

Struct layouts and function signatures may differ by platform (Windows vs Linux, 32-bit vs 64-bit). Use ffi.os and ffi.arch to conditionally declare correct signatures.

Safety Note

FFI bypasses Lua's type safety. Incorrect C declarations cause memory corruption, segfaults, or undefined behavior. Always double-check declarations against the actual C header.

FFI Basics Question

What does ffi.cdef do?

Recap: FFI Basics

Use ffi.cdef to declare C types/functions and ffi.load to load shared libraries. ffi.C gives direct access to libc. Always verify declarations match actual C headers to avoid memory corruption.

Frequently asked questions

Is the “FFI Basics: ffi.cdef and ffi.load” lesson free?

Yes — the full text of “FFI Basics: ffi.cdef and ffi.load” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “FFI Basics: ffi.cdef and ffi.load”?

Declare C types and load shared libraries with LuaJIT's ffi module. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “FFI Basics: ffi.cdef and ffi.load” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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