On OS X Snow Leopard, you may have libs of different architectures installed and many binaries are universal binaries, meaning that they are cross compiled for several architectures, and the best architectures is selected at run time.
This may cause issues when you go to run your FFI code using 64bit ruby against a lib that is 32 bit for example you may get an error similar to this :
/Library/Ruby/Gems/1.8/gems/ffi-0.6/lib/ffi/library.rb:61:in
`ffi_lib’: Could not open library ‘libmylib.dylib’:
dlopen(libmylib.dylib, 9): no suitable image found. Did find:
(LoadError) /usr/local/lib/libmylib.dylib: mach-o, but wrong architecture. Could
not open library ‘libmylib’: dlopen(libmylib, 9): image not found from /Library/Ruby/Gems/1.8/gems/ffi-0.6.2/lib/ffi/library.rb:
43:in `map’ from /Library/Ruby/Gems/1.8/gems/ffi-0.6/lib/ffi/library.rb:
43:in `ffi_lib’ from xxx.rb:14
Using lipo (man page for lipo) to examine the lib an find out what architecture it is, we can see that it is is i386)
$ lipo -info /usr/local/lib/libmylib.dylib
Non-fat file: /usr/local/lib/libmylib.dylib is architecture: i386
Running lipo on my stock install of Snow Leopard ruby, we can see that it is fact a fat binary(universal binary)
$ lipo -detailed_info /usr/bin/ruby
Fat header in: /usr/bin/ruby
fat_magic 0xcafebabe
nfat_arch 3
architecture x86_64
cputype CPU_TYPE_X86_64
cpusubtype CPU_SUBTYPE_X86_64_ALL
offset 4096
size 14176
align 2^12 (4096)
architecture i386
cputype CPU_TYPE_I386
cpusubtype CPU_SUBTYPE_I386_ALL
offset 20480
size 14112
align 2^12 (4096)
architecture ppc7400
cputype CPU_TYPE_POWERPC
cpusubtype CPU_SUBTYPE_POWERPC_7400
offset 36864
size 13904
align 2^12 (4096)
To force ruby to use the i386 arch we can use the arch command (man page for arch)
$ arch -arch i386 /usr/bin/ruby libmylib.rb
and everything shall work correctly.