Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created October 14, 2016 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/abfc407aeaf58de3b1b5bba64118f3eb to your computer and use it in GitHub Desktop.
Save JoshCheek/abfc407aeaf58de3b1b5bba64118f3eb to your computer and use it in GitHub Desktop.
MRuby compilation example
#!/usr/bin/env bash
set -eu
set -o pipefail
# variables
mrb_dir="/Users/josh/.rubies/mruby-1.2.0/"
export PATH="$mrb_dir/bin:$PATH"
# Clean up from previous run
rm -f simple_example*
# MRuby program that prints 0, 2, 4, 6, 8
( echo '5.times do |i|'
echo ' puts i * 2'
echo 'end'
) > simple_example.rb
# C program to run the mruby program
( echo '#include <stdio.h>'
echo '#include <mruby.h>'
echo '#include <mruby/dump.h>'
echo '#include "simple_example_bytecode.h"'
echo ''
echo 'int main() {'
echo ' mrb_state *mrb = mrb_open();'
echo ' if (!mrb) {'
echo ' printf("failed");'
echo ' return 1;'
echo ' }'
echo ' mrb_load_irep(mrb, simple_example_bytecode);'
echo ' mrb_close(mrb);'
echo '}'
) > simple_example.c
# Compile the Mruby code into a c header file
mrbc -B simple_example_bytecode -o simple_example_bytecode.h simple_example.rb
# compile to an object file
gcc -std=c99 -I"$mrb_dir/include" -c -o simple_example.o simple_example.c
# link object files
gcc -o simple_example simple_example.o "$mrb_dir/build/host/lib/libmruby.a"
# gcc -std=c99 -I"$mrb_dir/include" -o simple_example simple_example.c "$mrb_dir/build/host/lib/libmruby.a"
./simple_example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment