Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4615

How to "require" a third party module in C code when trying to call ruby methods from C?

$
0
0

I have the following code here:

#include <ruby.h>int main(int argc, char* argv[]){    /* construct the VM */    ruby_init();    /* Ruby goes here */    int state;    VALUE result;    result = rb_eval_string_protect("require 'strscan'", &state);    if (state)    {        /* handle exception */        VALUE exception = rb_errinfo();        rb_funcall(rb_mKernel, rb_intern("puts"), 1, exception); // Just print out the exception    }    /* destruct the VM */    return ruby_cleanup(0);}

and when I run this code, I get the following error:

cannot load such file -- strscaneval:1:in 'Kernel#require': cannot load such file -- strscan (LoadError)    from eval:1:in '<main>'

which obviously hints that the program can not find the library.

I compiled my code with these commands:

clang -I/home/oof/.rubies/ruby-master/include/ruby-3.4.0+0/x86_64-linux -I/home/oof/.rubies/ruby-master/include/ruby-3.4.0+0 -L/home/oof/.rubies/ruby-master/lib -lruby -lm oof.c -o binary

and then I also ran export LD_LIBRARY_PATH=/home/oof/.rubies/ruby-master/lib/ before running my code, since the program needs to find libruby.so to run.

I compiled ruby from source according to these instructions and installed that compiled version to /home/oof/.rubies/ruby-master/

How can I import strscan (or any third party ruby code) into C-ruby?

Thanks in advance for your answers.


Viewing all articles
Browse latest Browse all 4615

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>