Question

My C code is getting harder to manage due to the inflexibility of mkmf. For this reason, I'd like to use another build system.

What does rubygems need in order to build a C extension? How can I integrate a build system like autotools/configure into the workflow?

Gem::Specification.new 'my_gem' do |gem|
  # Will this work?
  gem.extensions = %w(ext/my_gem/configure)
end
Was it helpful?

Solution

If the C code is quite complicated (since you mention autotools and configure I assume it is) why don't you consider building separate C library, which is independent of Ruby? And then build a small and simple Ruby gem with the gluing code. Eventually the C library would become available in repositories for Debian or other Linux distributions and the maintenance of such a solution would be similar to all other gems, that are just wrappers for C libraries.

OTHER TIPS

There are some tools that help is such situation (e.g. rake-compiler gem), but I prefer to use RubyInline gem. It was designed to replace slow, performance critical sections of Ruby code with implementations in other languages (e.g. C is supported out of the box), but it is also used to inline code that calls external C libraries.

A RubyInline example looks as follows:

class MyClass
  inline(:C) do |builder|
    builder.include '<stdio.h>'
    builder.c <<-END
      void my_printf(char * string){
        printf("%s\\n",string);
      }
    END
  end
end
MyClass.new.my_printf("Abc") 
# prints 'Abc'

The nice feature of RubyInline is that you don't have to keep separate files for C and Ruby, some basic argument conversions are supported out of the box and you don't have to write the gluing code. The bad parts are that you don't have the full control over compilation, etc. Personally I find RubyInline a very powerful solution.

Take a look at ruby-ffi
It links to existing libraries so does not force any directory structure

Also http://guides.rubygems.org/c-extensions/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top