Run ruby script in C

Asked

Viewed 75 times

1

The error code when running the script in ruby.

file.Rb:

#!/usr/bin/env ruby

x=1
while x<10
    puts "It Work!"
    x+=1
end

main. c:

#include <ruby-2.0.0/ruby.h>

int main(int argc, char **argv){
    ruby_init();

    ruby_script("file.rb");
    void *node = rb_load_file("file.rb");
    ruby_run_node(node); // O erro se origina nessa linha

    ruby_finalize();

    return 0;
}

Error:

file.rb: [BUG] Segmentation fault
ruby 2.0.0p647 (2015-08-18 revision 51631) [x86_64-linux]

-- Control frame information -----------------------------------------------
c:0001 p:0000 s:0002 E:0009b8 TOP    [FINISH]


-- C level backtrace information -------------------------------------------
/usr/lib64/libruby20.so.2.0(+0x1786cb) [0x7fcb381966cb]
/usr/lib64/libruby20.so.2.0(+0x64a6a) [0x7fcb38082a6a]
/usr/lib64/libruby20.so.2.0(rb_bug+0xb3) [0x7fcb38083173]
/usr/lib64/libruby20.so.2.0(+0x111954) [0x7fcb3812f954]
/lib64/libpthread.so.0(+0x10740) [0x7fcb37e12740]
/usr/lib64/libruby20.so.2.0(+0x1618dc) [0x7fcb3817f8dc]
/usr/lib64/libruby20.so.2.0(rb_iseq_eval_main+0x57) [0x7fcb38192357]
/usr/lib64/libruby20.so.2.0(+0x67ff6) [0x7fcb38085ff6]
/usr/lib64/libruby20.so.2.0(ruby_exec_node+0x1d) [0x7fcb380876ed]
/usr/lib64/libruby20.so.2.0(ruby_run_node+0x1e) [0x7fcb380891fe]
./a.out() [0x40086e]
/lib64/libc.so.6(__libc_start_main+0xf0) [0x7fcb371417b0]
./a.out() [0x400769]

-- Other runtime information -----------------------------------------------

* Loaded script: file.rb

* Loaded features:

    0 enumerator.so

* Process memory map:

00400000-00401000 r-xp 00000000 08:06 32636932                           /mnt/hd_6/user_files/Projects/C_C++/Exemplos/Ruby/run_file/a.out
00600000-00601000 r--p 00000000 08:06 32636932                           /mnt/hd_6/user_files/Projects/C_C++/Exemplos/Ruby/run_file/a.out
00601000-00602000 rw-p 00001000 08:06 32636932                           /mnt/hd_6/user_files/Projects/C_C++/Exemplos/Ruby/run_file/a.out
014ae000-01614000 rw-p 00000000 00:00 0                                  [heap]
7fcb36f0a000-7fcb36f20000 r-xp 00000000 08:01 1451507                    /usr/lib64/gcc/x86_64-pc-linux-gnu/5.3.0/libgcc_s.so.1
7fcb36f20000-7fcb3711f000 ---p 00016000 08:01 1451507                    /usr/lib64/gcc/x86_64-pc-linux-gnu/5.3.0/libgcc_s.so.1
7fcb3711f000-7fcb37120000 r--p 00015000 08:01 1451507                    /usr/lib64/gcc/x86_64-pc-linux-gnu/5.3.0/libgcc_s.so.1
7fcb37120000-7fcb37121000 rw-p 00016000 08:01 1451507                    /usr/lib64/gcc/x86_64-pc-linux-gnu/5.3.0/libgcc_s.so.1
7fcb37121000-7fcb372b2000 r-xp 00000000 08:01 9437453                    /lib64/libc-2.21.so
7fcb372b2000-7fcb374b2000 ---p 00191000 08:01 9437453                    /lib64/libc-2.21.so
7fcb374b2000-7fcb374b6000 r--p 00191000 08:01 9437453                    /lib64/libc-2.21.so
7fcb374b6000-7fcb374b8000 rw-p 00195000 08:01 9437453                    /lib64/libc-2.21.so
7fcb374b8000-7fcb374bc000 rw-p 00000000 00:00 0 
7fcb374bc000-7fcb375be000 r-xp 00000000 08:01 9437326                    /lib64/libm-2.21.so
7fcb375be000-7fcb377bd000 ---p 00102000 08:01 9437326                    /lib64/libm-2.21.so
7fcb377bd000-7fcb377be000 r--p 00101000 08:01 9437326                    /lib64/libm-2.21.so
7fcb377be000-7fcb377bf000 rw-p 00102000 08:01 9437326                    /lib64/libm-2.21.so
7fcb377bf000-7fcb377c7000 r-xp 00000000 08:01 9437459                    /lib64/libcrypt-2.21.so
7fcb377c7000-7fcb379c6000 ---p 00008000 08:01 9437459                    /lib64/libcrypt-2.21.so
7fcb379c6000-7fcb379c7000 r--p 00007000 08:01 9437459                    /lib64/libcrypt-2.21.so
7fcb379c7000-7fcb379c8000 rw-p 00008000 08:01 9437459                    /lib64/libcrypt-2.21.so
7fcb379c8000-7fcb379f6000 rw-p 00000000 00:00 0 
7fcb379f6000-7fcb379f8000 r-xp 00000000 08:01 9437340                    /lib64/libdl-2.21.so
7fcb379f8000-7fcb37bf8000 ---p 00002000 08:01 9437340                    /lib64/libdl-2.21.so
7fcb37bf8000-7fcb37bf9000 r--p 00002000 08:01 9437340                    /lib64/libdl-2.21.so
7fcb37bf9000-7fcb37bfa000 rw-p 00003000 08:01 9437340                    /lib64/libdl-2.21.so
7fcb37bfa000-7fcb37c00000 r-xp 00000000 08:01 9437461                    /lib64/librt-2.21.so
7fcb37c00000-7fcb37e00000 ---p 00006000 08:01 9437461                    /lib64/librt-2.21.so
7fcb37e00000-7fcb37e01000 r--p 00006000 08:01 9437461                    /lib64/librt-2.21.so
7fcb37e01000-7fcb37e02000 rw-p 00007000 08:01 9437461                    /lib64/librt-2.21.so
7fcb37e02000-7fcb37e19000 r-xp 00000000 08:01 9437244                    /lib64/libpthread-2.21.so
7fcb37e19000-7fcb38018000 ---p 00017000 08:01 9437244                    /lib64/libpthread-2.21.so
7fcb38018000-7fcb38019000 r--p 00016000 08:01 9437244                    /lib64/libpthread-2.21.so
7fcb38019000-7fcb3801a000 rw-p 00017000 08:01 9437244                    /lib64/libpthread-2.21.so
7fcb3801a000-7fcb3801e000 rw-p 00000000 00:00 0 
7fcb3801e000-7fcb38254000 r-xp 00000000 08:01 552009                     /usr/lib64/libruby20.so.2.0.0
7fcb38254000-7fcb38453000 ---p 00236000 08:01 552009                     /usr/lib64/libruby20.so.2.0.0
7fcb38453000-7fcb38458000 r--p 00235000 08:01 552009                     /usr/lib64/libruby20.so.2.0.0
7fcb38458000-7fcb3845b000 rw-p 0023a000 08:01 552009                     /usr/lib64/libruby20.so.2.0.0
7fcb3845b000-7fcb3847c000 rw-p 00000000 00:00 0 
7fcb3847c000-7fcb3849e000 r-xp 00000000 08:01 9437444                    /lib64/ld-2.21.so
7fcb38561000-7fcb38667000 rw-p 00000000 00:00 0 
7fcb38697000-7fcb38698000 rw-p 00000000 00:00 0 
7fcb38698000-7fcb38699000 ---p 00000000 00:00 0 
7fcb38699000-7fcb3869d000 rw-p 00000000 00:00 0                          [stack:1615]
7fcb3869d000-7fcb3869e000 r--p 00021000 08:01 9437444                    /lib64/ld-2.21.so
7fcb3869e000-7fcb3869f000 rw-p 00022000 08:01 9437444                    /lib64/ld-2.21.so
7fcb3869f000-7fcb386a0000 rw-p 00000000 00:00 0 
7ffc603fb000-7ffc6041d000 rw-p 00000000 00:00 0 
7ffc6048c000-7ffc6048e000 r--p 00000000 00:00 0                          [vvar]
7ffc6048e000-7ffc60490000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]


[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html

Abortado

What’s wrong with the code? ruby (2.0.0)

The error may be something related to the OS or ruby version?

  • The lib you are using is 64 or 32 bit?

  • I am using 64x, I tried to run with ruby 2.1 and 2.0, but it was the same

1 answer

0


So you have the same problem importing the script.

Some methods have become obsolete since version 1.8.0, to run a file in version 2.x.0 just use the method rb_require.

ruby_setup();
rb_require("./file"); // ou "./file.rb"
ruby_finalize();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.