ffi/ffi-compiler
null
{ "createdAt": "2013-01-15T20:01:02Z", "defaultBranch": "master", "description": null, "fullName": "ffi/ffi-compiler", "homepage": null, "language": "Ruby", "name": "ffi-compiler", "pushedAt": "2024-12-25T15:44:50Z", "stargazersCount": 37, "topics": [], "updatedAt": "2025-10-23T11:15:56Z", "url": "https://github.com/ffi/ffi-compiler"}ffi-compiler is a ruby library for automating compilation of native libraries for use with ffi
To use, define your own ruby->native API using ffi, implement it in C, then use ffi-compiler to compile it.
Example
Section titled “Example”Directory layout
Section titled “Directory layout”lib |- example |- example.rb
ext |- example.c |- Rakefile
example.gemspeclib/example/example.rb
Section titled “lib/example/example.rb”require 'ffi'require 'ffi-compiler/loader'
module Example extend FFI::Library ffi_lib FFI::Compiler::Loader.find('example')
# example function which takes no parameters and returns long attach_function :example, [], :longendext/example.c
Section titled “ext/example.c”longexample(void){ return 0xdeadbeef;}ext/Rakefile
Section titled “ext/Rakefile”require 'ffi-compiler/compile_task'
FFI::Compiler::CompileTask.new('example') do |c| c.have_header?('stdio.h', '/usr/local/include') c.have_func?('puts') c.have_library?('z')endexample.gemspec
Section titled “example.gemspec”Gem::Specification.new do |s| s.extensions << 'ext/Rakefile' s.name = 'example' s.version = '0.0.1' s.email = 'ffi-example' s.files = %w(example.gemspec) + Dir.glob("{lib,spec,ext}/**/*") s.add_dependency 'rake' s.add_dependency 'ffi-compiler'endBuild gem and install it
Section titled “Build gem and install it”gem build example.gemspec && gem install example-0.0.1.gemSuccessfully built RubyGem Name: example Version: 0.0.1 File: example-0.0.1.gemBuilding native extensions. This could take a while...Successfully installed example-0.0.1Test it
Section titled “Test it”$ irb2.0.0dev :001 > require 'example/example' => true2.0.0dev :002 > puts "Example.example=#{Example.example.to_s(16)}"Example.example=deadbeef => nil