Is there a way to point Ruby FFI to a header file instead of writing attach_function calls? A header file basically has the same exact information.
temp_convert.rb:
attach_function :temp_convert, [:float], :float
temp_convert.h:
float 开发者_如何学运维temp_convert(float temp);
Because C header files are written in C but Ruby interpreters only interpret Ruby. Also, the header files might not even be available at runtime.
There has been talk about automatically generating attach_function
calls from headers. However, as I hinted at above, this basically means that you must implement a full C compiler (well, the full front half of one, to be precise). At the moment, the Ruby implementers are more focused on making Ruby run as fast as C to alleviate the need to use FFI in the first place than writing their own C compiler (which is a non-trivial undertaking even though you only need to do the lexing, parsing, semantic analysis and typing parts, not the actual code generation or optimization).
As Jörg says, implementing a header scanner means implementing quite a bit of the C compiler, to get everything right.
One thing you might like to try to ease the pain is the FFI Swig Generator. It uses swig to generate the FFI interface. It still means you need to do a bit of work, which can boil down to a cut'n'paste job to generate the swig input file for simple interfaces.
精彩评论