Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Created October 22, 2011 02:00
Show Gist options
  • Save jefftrull/1305431 to your computer and use it in GitHub Desktop.
Save jefftrull/1305431 to your computer and use it in GitHub Desktop.
Example of embedding Ruby interpreter into C/C++ program
// very basic embedded Ruby interpreter for C++ programs
// thrown together by Jeff Trull <[email protected]> based on googling and asking questions on #ruby-lang
#include <ruby.h>
// access to C variables
static VALUE getter(VALUE ns, VALUE var) {
return Qfalse;
}
VALUE setter(VALUE ns, VALUE var, VALUE val) {
return Qfalse;
}
RUBY_GLOBAL_SETUP
int main(int argc, char **argv) {
ruby_sysinit(&argc, &argv);
RUBY_INIT_STACK;
ruby_init();
// provide interactive prompt with irb (the Ruby shell) by telling ruby to execute it on startup
// copy arg array and add this option at the end
char** new_argv = (char**)malloc((argc + 2) * sizeof(char*));
for (int i = 0; i < argc; i++) {
new_argv[i] = argv[i];
}
new_argv[argc] = "-e";
new_argv[argc+1] = "require \"irb\"; IRB.start";
// create some modules
VALUE mymod = rb_define_module("MOD");
rb_define_module_function(mymod, "setparam", setter, 3);
rb_define_module_function(mymod, "getparam", getter, 2);
return ruby_run_node(ruby_options(argc+2, new_argv));
}
@jalcine
Copy link

jalcine commented Mar 13, 2013

@jlebrech: You can do it by using the following (assuming that you have Ruby's headers installed on your system with Ruby 1.9.3-p286):

$ g++ main.cpp `pkg-config --cflags --libs ruby-1.9` -o testapp

However, I'm getting an error:

 $ g++ -c main.cpp `pkg-config --cflags --libs ruby-1.9`
main.cpp: In function ‘int main(int, char**)’:
main.cpp:27:20: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
main.cpp:28:22: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
main.cpp:32:58: error: invalid conversion from ‘VALUE (*)(VALUE, VALUE, VALUE) {aka long unsigned int (*)(long unsigned int, long unsigned int, long unsigned int)}’ to ‘VALUE (*)(...) {aka long unsigned int (*)(...)}’ [-fpermissive]
In file included from /home/jacky/.local/include/ruby-1.9.1/ruby.h:32:0,
                 from main.cpp:3:
/home/jacky/.local/include/ruby-1.9.1/ruby/ruby.h:1101:6: error:   initializing argument 3 of ‘void rb_define_module_function(VALUE, const char*, VALUE (*)(...), int)’ [-fpermissive]
main.cpp:33:58: error: invalid conversion from ‘VALUE (*)(VALUE, VALUE) {aka long unsigned int (*)(long unsigned int, long unsigned int)}’ to ‘VALUE (*)(...) {aka long unsigned int (*)(...)}’ [-fpermissive]
In file included from /home/jacky/.local/include/ruby-1.9.1/ruby.h:32:0,
                 from main.cpp:3:
/home/jacky/.local/include/ruby-1.9.1/ruby/ruby.h:1101:6: error:   initializing argument 3 of ‘void rb_define_module_function(VALUE, const char*, VALUE (*)(...), int)’ [-fpermissive]

@AndrewBelt
Copy link

I am getting the same compile error with the variadic arguments.

@xR3b0rn
Copy link

xR3b0rn commented Dec 23, 2013

You have to reininterpret-cast the argument you use:
rb_define_module_function(mymod, "setparam", reininterpret_cast< VALUE ( * ) ( ... ) >(setter), 3);
I do not say anything wrong, but so far as i know it's a C to C++ problem.

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