This guide walks you through generating Ruby FFI bindings for a C library.
Create a file named ffi-bindings.yaml:
project: mylib
input: ./include
output: ./lib/generated
format: FFI
match:
- "**/*.h"
library_names:
- mylib
clang:
args:
- -I./include
- -xcKey options:
project— name used for the project file (mylib_ffi.rb) and the Ruby moduleinput— directory containing header filesoutput— where generated Ruby files are writtenlibrary_names— shared library names to load at runtime (e.g.,mylibforlibmylib.so)match— glob patterns selecting which headers to processclang.args— compiler arguments;-xctells clang to parse as C
Paths are relative to the config file's directory, not the working directory.
See Configuration for all options, including library versioning, symbol filtering, and version guards.
ruby-bindgen ffi-bindings.yamlThis produces a project file, one Ruby file per matched header, and optionally a version stub file. See FFI Output for details.
require_relative 'lib/generated/mylib_ffi'
result = Mylib.my_function(42, "hello")The project file (mylib_ffi.rb) loads the native library and requires all content files. You only need to require the project file.