mruby is a ruby implementation that allows compiling ruby code, unfortunately there's not so much documentation around therefore this is a dump of the info I gather for my personal use.
In order to compile a ruby library that uses the require
syntax.
git clone https://github.com/mruby/mruby.git
build_config/default.rb
to :MRuby::Build.new do |conf|
if ENV['OS'] != 'Windows_NT' then
conf.cc.flags << %w|-fPIC| # needed for using bundled gems
end
conf.toolchain
conf.gembox 'default'
conf.enable_bintest
conf.enable_test
conf.gem :github => 'mattn/mruby-require'
end
rake
in the root folderbuild/host/bin
mruby is an emdedded version of ruby, here's how to compile a simple ruby file into C file. This memo is using mruby's official compiling guide.
Using rbenv :
rbenv install -l
mruby-2.1.2
:
rbenv install mruby-2.1.2
mkdir my_project
cd my_project
rbenv local mruby-2.1.2
You need the following files in the project folder:
test.rb
:puts "this is a test"
test_stub.c
:#include <mruby.h>
#include <mruby/irep.h>
#include "test.c"
int
main(void)
{
mrb_state *mrb = mrb_open();
if (!mrb) { /* handle error */ }
mrb_load_irep(mrb, test_symbol);
mrb_close(mrb);
return 0;
}
Compiling to c file is straight forward :
mrbc -Btest_symbol test.rb
should produce a file named test.c
using gcc you can compile you test.c
like that :
gcc -std=c99 -I /Users/username/.rbenv/versions/mruby-2.1.2/include/ test_stub.c -o test_program /Users/username/.rbenv/versions/mruby-2.1.2/lib/libmruby.a -lm
./test_program
should print this is a test