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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With