title: How to use ruby ffi for calling custom c code in a ruby gem
body: <p>I want to create a ruby gem that calls a c function I have written. For example:</p>
  
  <pre><code>int func(int x)
  {
    return x * 2;
  }
  </code></pre>
  
  <p>This would be in a c file in my rubygem. I would then want to be able to call it in ruby, by wrapping it with the ffi interface:</p>
  
  <pre><code>  module TestModule
      extend FFI::Library
      ffi_lib 'MyLib'
      attach_function 'func', [:int], :int
    end
  </code></pre>
  
  <p>However I'm not sure how to set this up, so that when the gem is installed the c file would compile into a library (with the name 'MyLib'), and the ffi will detect the library and use it in the code above. </p>
  
  <p>Does anyone know how to do this, or is there a better way of going about this? Note that I would rather not use the standard way of extending ruby (as described in the The <a href="http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html" rel="nofollow">Pragmatic Programmer's Guide Extending Ruby section</a>) since that only will work with the standard ruby interpretor (I believe).</p>
  
  <p>I have been using <a href="https://github.com/technicalpickles/jeweler" rel="nofollow">jeweler</a> for building my gems, if that matters for this question.</p>