I'm trying to call Ruby from C, and for some reason I cannot execute rb_funcall2
nor rb_funcall3
. However rb_funcall
works.
My example,
Hello.c
#include <ruby.h>
void hello_from_ruby()
{
if (ruby_setup())
{
fprintf(stderr, "Failed to init Ruby VM\n");
}
rb_require("/test");
rb_funcall2(0, rb_intern("some_ruby_method"), 0, NULL);
ruby_cleanup(0);
}
int main(int argc, char* argv[])
{
hello_from_ruby();
return 0;
}
test.rb
def some_ruby_method
puts "Hello from ruby"
end
Command that execute:
clang hello.c -o hello -I/opt/rubies/2.1.0/include/ruby-2.1.0 -I/opt/rubies/2.1.0/include/ruby-2.1.0/x86_64-darwin14.0 -lruby
The exception:
Undefined symbols for architecture x86_64:
"_rb_funcallv", referenced from:
_hello_from_ruby in hello-190761.o
ld: symbol(s) not found for architecture x86_64
For some reason, only these two methods are not being linked. Thanks
My question: How can I link rb_funcall2
on my code
The problem was that my Ruby was compiled using gcc
so I had to recompile for clang:
CC=clang CONFIGURE_OPTS="--with-gcc=clang --enable-shared" ruby-build 2.1.0 /opt/rubies/2.1.0
I figure it out after looking into this issue:https://github.com/ryanmelt/qtbindings/issues/72
Thanks.