Skip to content

Instantly share code, notes, and snippets.

@xavriley
Created March 23, 2016 19:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xavriley/507eff0a75d4552fa56e to your computer and use it in GitHub Desktop.
Save xavriley/507eff0a75d4552fa56e to your computer and use it in GitHub Desktop.
Writing a C extension for Ruby to parse and unparse OSC messages

This is a note for myself more than anything. I've started on a wrapper for the rtosc library https://github.com/fundamental/rtosc

The aim is to make a gem called FastOsc that will have two methods

FastOsc.serialise(["/aa", "foo", "bar"]) #=> "/aa\x00ss\x00\x00foo\x00bar\x00"
FastOsc.deserialise("/aa\x00ss\x00\x00foo\x00bar\x00") #=> ["foo", "bar"]

This followed from the rtosc library author giving me a really helpful response. fundamental/rtosc#28

A selection of sources that helped with this

Linking static/dynamic libraries http://c.learncodethehardway.org/book/ex29.html dyld lazy symbol binding failed: symbol not found postmodern/ffi-hunspell#10 (I was linking the headers but not the libs .a file) Super useful post on setting up extconf.rb to find headers and libs http://blog.zachallett.com/howto-ruby-c-extension-with-a-static-library/ implicit self and "what does VALUE return?" http://stackoverflow.com/questions/7050800/ruby-c-extensions-api-questions

Ruby C API guides

This jcoglan blog was super useful at bootstrapping a gem with ext and rake-compile https://blog.jcoglan.com/2012/07/29/your-first-ruby-native-extension-c/ Tenderlove blog posts http://tenderlovemaking.com/2009/06/26/string-encoding-in-ruby-1-9-c-extensions.html The guide (README.EXT) https://github.com/ruby/ruby/blob/trunk/doc/extension.rdoc a bit out of date now http://blog.x-aeon.com/2012/12/13/the-ruby-c-api-basics/#TRCAB_Strings pdf guide http://java.ociweb.com/mark/NFJS/RubyCExtensions.pdf Pickaxe section http://phrogz.net/programmingruby/ext_ruby.html

Examples

xml4r (early nokogiri?) https://github.com/xml4r/libxslt-ruby/blob/master/ext/libxslt/ruby_exslt.c example of StringValuePtr for constant char array srawlins/gmp#6

FFI stuff that I didn't end up going with

https://spin.atomicobject.com/2010/01/11/making-friends-with-ffi/ https://spin.atomicobject.com/2013/02/15/ffi-foreign-function-interfaces/ FFI over liblo https://github.com/erdg/picolisp-osc/blob/master/liblo.l Flexible array members (size_t) seem tricky in FFI ffi/ffi#436

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment