Subject:
[ruby-ffi] Unable to load .so file - any ideas?
From:
Tony Summerville
Date:
3/6/12 9:10 AM
To:
ruby-ffi@googlegroups.com

I'd really appreciate any help or thoughts on this issue.

I'm trying to wrap a simple shared library in a Ruby Gem. The .so file was precompiled by a 3rd-party vendor and I don't have access to the C source files.

I've installed FFI gem v1.0.11 and am able to get the basic "getpid" example working perfectly, but when I try to load the .so file I get the following error:

ruby-1.9.2-p180 :001 > require "prova-vhm-raw2xml"
LoadError: Could not open library '/Users/tsummerville/.rvm/gems/ruby-1.9.2-p180@prova-vhm-raw2xml/gems/prova-vhm-raw2xml-0.1.0/lib/libZVLParser64/libZVLParser64.so': dlopen(/Users/tsummerville/.rvm/gems/ruby-1.9.2-p180@prova-vhm-raw2xml/gems/prova-vhm-raw2xml-0.1.0/lib/libZVLParser64/libZVLParser64.so, 5): no suitable image found.  Did find:
/Users/tsummerville/.rvm/gems/ruby-1.9.2-p180@prova-vhm-raw2xml/gems/prova-vhm-raw2xml-0.1.0/lib/libZVLParser64/libZVLParser64.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00
from /Users/tsummerville/.rvm/gems/ruby-1.9.2-p180@prova-vhm-raw2xml/gems/ffi-1.0.11/lib/ffi/library.rb:121:in `block in ffi_lib'
from /Users/tsummerville/.rvm/gems/ruby-1.9.2-p180@prova-vhm-raw2xml/gems/ffi-1.0.11/lib/ffi/library.rb:88:in `map'
from /Users/tsummerville/.rvm/gems/ruby-1.9.2-p180@prova-vhm-raw2xml/gems/ffi-1.0.11/lib/ffi/library.rb:88:in `ffi_lib'
from /Users/tsummerville/.rvm/gems/ruby-1.9.2-p180@prova-vhm-raw2xml/gems/prova-vhm-raw2xml-0.1.0/lib/prova-vhm-raw2xml.rb:9:in `<module:Raw2Xml>'
from /Users/tsummerville/.rvm/gems/ruby-1.9.2-p180@prova-vhm-raw2xml/gems/prova-vhm-raw2xml-0.1.0/lib/prova-vhm-raw2xml.rb:6:in `<module:ProvaVHM>'
from /Users/tsummerville/.rvm/gems/ruby-1.9.2-p180@prova-vhm-raw2xml/gems/prova-vhm-raw2xml-0.1.0/lib/prova-vhm-raw2xml.rb:5:in `<module:Fleetio>'
from /Users/tsummerville/.rvm/gems/ruby-1.9.2-p180@prova-vhm-raw2xml/gems/prova-vhm-raw2xml-0.1.0/lib/prova-vhm-raw2xml.rb:4:in `<top (required)>'
from /Users/tsummerville/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
from /Users/tsummerville/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
from /Users/tsummerville/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from (irb):1
from /Users/tsummerville/.rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `<main>'

Here's the relevant directories/files in my Ruby Gem:

/prova-vhm-raw2xml
  /lib
    /prova-vhm-raw2xml.rb
    /libZVLParser64
      /libZVLParser64.so

Here's the relevant code in prova-vhm-raw2xml.rb:

require 'ffi'

module ProvaVHM
  module Raw2Xml
    extend FFI::Library
  
    ffi_lib File.expand_path(File.dirname(__FILE__) + '/libZVLParser64/libZVLParser64.so')
  
    # THIS WORKS FINE.
    # ffi_lib FFI::Library::LIBC
    # attach_function :getpid, [], :int
  
    def self.hi
      puts "Hola!"
    end
  end
end

Other potentially relevant notes:
- I'm on Mac OSX with 64 bit processor
- The libZVLParser64.so was compiled for 64 bit architecture

Anything obvious that I'm missing? I'm new to FFI and wrapping C libs with Ruby in general, so I'm just not quite sure what I'm doing wrong. I've read through all the documentation and looked through the Google Group but can't figure out what the problem is.

Thanks again for any help or insight you can provide!