embedding, 'require' and extensions

Discussion in 'Ruby' started by Phil Tomson, Oct 30, 2004.

  1. Phil Tomson

    Phil Tomson Guest

    I've basically taken the embedding example from pickaxeII and the only
    thing I've added is for the Ruby program to do a 'require' of a file known
    to exist in the library.

    Here is the relevant part of the C-side code:
    //summer.c
    int main(void) {
    int value;
    int* next = Values;

    ruby_init();
    ruby_init_loadpath();
    ruby_script("embedded");
    rb_require("sum.rb"); //ruby script is sum.rb

    ruby_finalize();
    exit(0);
    }
    //end of summer.c

    So the ruby_init_loadpath() should be setting up the loadpath correctly,
    and I can confirm this by making the ruby script to be required (sum.rb)
    like so:

    #sum.rb
    puts $:

    Now if I run summer, I get:
    $ ./summer
    /usr/local/lib/ruby/site_ruby/1.8
    /usr/local/lib/ruby/site_ruby/1.8/i686-linux
    /usr/local/lib/ruby/site_ruby
    /usr/local/lib/ruby/1.8
    /usr/local/lib/ruby/1.8/i686-linux
    Phil Tomson, Oct 30, 2004
    #1
    1. Advertising

  2. Phil Tomson

    ts Guest

    >>>>> "P" == Phil Tomson <> writes:

    P> Here is the relevant part of the C-side code:

    You don't give the most important part : how do you build your
    executable ?

    You must use the value given in rbconfig.rb to build the command line, in
    your case you've probably forgotten -rdynamic when you have compiled your
    source.

    Another thing

    P> rb_require("sum.rb"); //ruby script is sum.rb

    You *must* protect any call to a ruby function which can generate an
    error, with rb_protect(), otherwise ruby will crash




    Guy Decoux
    ts, Oct 30, 2004
    #2
    1. Advertising

  3. Phil Tomson

    Phil Tomson Guest

    In article <>,
    ts <> wrote:
    >>>>>> "P" == Phil Tomson <> writes:

    >
    >P> Here is the relevant part of the C-side code:
    >
    > You don't give the most important part : how do you build your
    > executable ?
    >
    > You must use the value given in rbconfig.rb to build the command line, in
    > your case you've probably forgotten -rdynamic when you have compiled your
    > source.


    Here's how I built:
    $ gcc -I/usr/local/lib/ruby/1.8/i686-linux -g -c -o summer.o summer.c
    $ gcc -o summer summer.o -L/usr/local/lib/ruby/1.8/i686-linux -lruby-static -ldl -lm -lcrypt
    -rdynamic

    It didn't make any difference if I used rdynamic or not.

    >
    > Another thing
    >
    >P> rb_require("sum.rb"); //ruby script is sum.rb
    >
    > You *must* protect any call to a ruby function which can generate an
    > error, with rb_protect(), otherwise ruby will crash
    >


    Yes, I know. I was only doing a quick/small test before adding more.

    Phil
    Phil Tomson, Oct 30, 2004
    #3
  4. Phil Tomson

    ts Guest

    >>>>> "P" == Phil Tomson <> writes:

    P> $ gcc -I/usr/local/lib/ruby/1.8/i686-linux -g -c -o summer.o summer.c
    P> $ gcc -o summer summer.o -L/usr/local/lib/ruby/1.8/i686-linux -lruby-static -ldl -lm -lcrypt
    P> -rdynamic

    P> It didn't make any difference if I used rdynamic or not.

    Well, at moulon


    svg% cat aa.c
    #include <ruby.h>

    static VALUE
    tt()
    {
    return rb_require("sum.rb");
    }

    int main()
    {
    int value;

    ruby_init();
    ruby_init_loadpath();
    ruby_script("embedded");
    rb_protect(tt, Qnil, &value);
    if (value) {
    VALUE err = rb_inspect(rb_gv_get("$!"));
    fprintf(stderr, "ERR %s\n", StringValuePtr(err));
    }

    ruby_finalize();
    exit(0);
    }
    svg%

    svg% cc -I. -I/usr/local/lib/ruby/1.8/i686-linux aa.c -L/usr/local/lib -lruby-static -ldl -lcrypt -lm
    svg%

    svg% ./a.out
    ERR #<LoadError: /usr/local/lib/ruby/1.8/i686-linux/openssl.so: undefined symbol: rb_eRuntimeError - /usr/local/lib/ruby/1.8/i686-linux/openssl.so>
    svg%

    svg% cc -I. -I/usr/local/lib/ruby/1.8/i686-linux aa.c -L/usr/local/lib -lruby-static -ldl -lcrypt -lm -rdynamic
    svg%

    svg% ./a.out
    svg%




    Guy Decoux
    ts, Oct 31, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    295
  2. Axel Diener
    Replies:
    0
    Views:
    338
    Axel Diener
    Feb 11, 2004
  3. Rurouni
    Replies:
    1
    Views:
    99
    Kouhei Sutou
    Mar 6, 2006
  4. Remi Gillig

    Loading extensions and embedding

    Remi Gillig, Jun 9, 2008, in forum: Ruby
    Replies:
    0
    Views:
    73
    Remi Gillig
    Jun 9, 2008
  5. Albert Schlef

    Extensions and 'require'

    Albert Schlef, Feb 2, 2009, in forum: Ruby
    Replies:
    6
    Views:
    91
    Albert Schlef
    Feb 3, 2009
Loading...

Share This Page