[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

require 'tk' SEGV with Ruby embedded in C

Nicolas Desprès

3/24/2006 7:49:00 AM

Hi there,

I'm having trouble trying to embed Ruby in a C program.

$ cat test.c
#include <ruby.h>

int main()
{
ruby_init();
ruby_script("test");
ruby_init_loadpath();
rb_eval_string("require 'tk'");
return 0;
}
$ gcc -o test test.c -L/opt/ruby-1.8.4/lib -lruby
-I/opt/ruby-1.8.4/lib/ruby/1.8/i686-linux
$ ./test
/opt/ruby-1.8.4/lib/ruby/site_ruby/1.8/i686-linux/tcltklib.so: [BUG]
Segmentation fault
ruby 1.8.4 (2005-12-24) [i686-linux]

zsh: abort ./test
$ ruby -e "require 'tk'"
$

As you can check require 'tk' works fine using ruby -e, but it fails
with the C version. I've check requiring others files such as
thread.rb, pathname.rb, digest.so, readline.so, dl.so, ... and they
all work with both ruby -e and my C program. I don't understand what
I'm doing wrong in my C code.

Any help, would be great,

Cheers,

--
Nicolas Desprès


3 Answers

Nicolas Desprès

3/24/2006 9:47:00 AM

0

On 3/24/06, Nicolas Desprès <nicolas.despres@gmail.com> wrote:
> I'm having trouble trying to embed Ruby in a C program.
>

Patching the program this way, solve the problem:

> $ cat test.c
> #include <ruby.h>
>

EXTERN VALUE rb_progname;
EXTERN VALUE rb_argv0;

> int main()
> {
> ruby_init();
> ruby_script("test");
rb_argv0 = rb_progname;
> ruby_init_loadpath();
> rb_eval_string("require 'tk'");
> return 0;
> }

I'm looking for a cleaner solution.

Regards,

--
Nicolas Desprès


Nicolas Desprès

3/24/2006 10:41:00 AM

0

On 3/24/06, Nicolas Desprès <nicolas.despres@gmail.com> wrote:
> On 3/24/06, Nicolas Desprès <nicolas.despres@gmail.com> wrote:
> > I'm having trouble trying to embed Ruby in a C program.
> >
>
> Patching the program this way, solve the problem:
>
> > $ cat test.c
> > #include <ruby.h>
> >
>
> EXTERN VALUE rb_progname;
> EXTERN VALUE rb_argv0;
>
> > int main()
> > {
> > ruby_init();
> > ruby_script("test");
> rb_argv0 = rb_progname;
> > ruby_init_loadpath();
> > rb_eval_string("require 'tk'");
> > return 0;
> > }
>
> I'm looking for a cleaner solution.
>

Indeed the problem comes from the tcltklib.c line 7932 and the program
fails since rb_argv0 is not initialized. I think the ruby_script
function should have initialized it since this function initializes
rb_progname and ruby_sourcefile.

Using ruby_options also solves the problem but ruby_options reads
stdin if no arguments are given and in fact I don't want to give
arguments since I just want to use ruby as a library. Finally, when I
look at ruby_process_options (called by ruby_options) I saw these two
lines at the beginning:

ruby_script(argv[0]); /* for the time being */
rb_argv0 = rb_progname;

I'm wondering why the second statements is not included in the
ruby_script function. If it were, it may have saved me a lot of time
by avoiding me the problem I mentioned above.

Any further commentaries about this issue is welcome,

Best regards,

--
Nicolas Desprès


habshi

2/6/2013 12:15:00 AM

0

Solar power now cheaper than coal

http://www.energyandcapital.com/articles/first-solar-sells-solar-power-cheaper-than...

El Paso Electric Co. (NYSE: EE) has agreed to buy electricity
produced at First Solar Inc.?s (NASDAQ: FSLR) Macho Springs project in
New Mexico for 5.79 cents/kilowatt-hour.

The price is a good bit less than 12.8 cents/kilowatt-hour, which (per
Bloomberg analyses) is the average cost for power from new coal
plants.

The Macho Springs project has a power capacity of 50 MW