Code Monkey home page Code Monkey logo

ffi-compiler's Introduction

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

Directory layout
lib
  |- example
      |- example.rb
      
ext
  |- example.c
  |- Rakefile
  
example.gemspec
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, [], :long
end
ext/example.c
long
example(void)
{
    return 0xdeadbeef;
}
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')
end
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'
end
Build gem and install it
gem build example.gemspec && gem install example-0.0.1.gem
Successfully built RubyGem
  Name: example
  Version: 0.0.1
  File: example-0.0.1.gem
Building native extensions.  This could take a while...
Successfully installed example-0.0.1
Test it
$ irb
2.0.0dev :001 > require 'example/example'
 => true 
2.0.0dev :002 > puts "Example.example=#{Example.example.to_s(16)}"
Example.example=deadbeef
 => nil 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.