Compiling c program using rb_eval_string()

I tried to call rb_eval_string(), so I wrote simple C code.

#include "/home/xopht/lib/1.8/i686-linux/ruby.h"

int main()
{
    rb_eval_string( "puts" );
    return 0;
}

I compiled it and get bellow result.

[xopht@odin ruby]$ g++ -o test test.cc -lruby-static -L/home/xopht/lib/
/home/xopht/lib//libruby-static.a(string.o)(.text+0x319a): In function
`rb_str_crypt':
/home/xopht/ruby/ruby-1.8.4/string.c:4360: undefined reference to
`crypt'
/home/xopht/lib//libruby-static.a(dln.o)(.text+0x9f): In function
`dln_load':
/home/xopht/ruby/ruby-1.8.4/dln.c:1351: undefined reference to `dlopen'
/home/xopht/lib//libruby-static.a(dln.o)(.text+0xb6):/home/xopht/ruby/ruby-1.8.4/dln.c:1356:
undefined reference to `dlsym'
/home/xopht/lib//libruby-static.a(dln.o)(.text+0x12b):/home/xopht/ruby/ruby-1.8.4/dln.c:1359:
undefined reference to `dlclose'
/home/xopht/lib//libruby-static.a(dln.o)(.text+0x5): In function
`dln_strerror':
/home/xopht/ruby/ruby-1.8.4/dln.c:1193: undefined reference to
`dlerror'
collect2: ld returned 1 exit status
[xopht@odin ruby]$

what's wrong?

rb_string_eval() needs to be initialized properly.
Check this,
http://phrogz.net/ProgrammingRuby/ext_ruby.html#extendingruby

···

On 2/23/06, hongseok.yoon@gmail.com <hongseok.yoon@gmail.com> wrote:

I tried to call rb_eval_string(), so I wrote simple C code.

#include "/home/xopht/lib/1.8/i686-linux/ruby.h"

int main()
{
    rb_eval_string( "puts" );
    return 0;
}

I compiled it and get bellow result.

[xopht@odin ruby]$ g++ -o test test.cc -lruby-static -L/home/xopht/lib/
/home/xopht/lib//libruby-static.a(string.o)(.text+0x319a): In function
`rb_str_crypt':
/home/xopht/ruby/ruby-1.8.4/string.c:4360: undefined reference to
`crypt'
/home/xopht/lib//libruby-static.a(dln.o)(.text+0x9f): In function
`dln_load':
/home/xopht/ruby/ruby-1.8.4/dln.c:1351: undefined reference to `dlopen'
/home/xopht/lib//libruby-static.a(dln.o)(.text+0xb6):/home/xopht/ruby/ruby-1.8.4/dln.c:1356:
undefined reference to `dlsym'
/home/xopht/lib//libruby-static.a(dln.o)(.text+0x12b):/home/xopht/ruby/ruby-1.8.4/dln.c:1359:
undefined reference to `dlclose'
/home/xopht/lib//libruby-static.a(dln.o)(.text+0x5): In function
`dln_strerror':
/home/xopht/ruby/ruby-1.8.4/dln.c:1193: undefined reference to
`dlerror'
collect2: ld returned 1 exit status
[xopht@odin ruby]$

what's wrong?

--
http://nohmad.sub-port.net

I compiled it and get bellow result.

This is in your C compiler. You probably need to add some library flags like
"-lcrypt -ldl" or others in order for the linker to be happy.

You guys are really helpful !!

I changed my code...

int main()
{
    ruby_init();
    rb_eval_string( "puts" );
    return 0;

}

and compiled it.

[xopht@odin ruby]$ g++ -o test test.cc -lruby-static -L/home/xopht/lib/
-lcrypt -ldl

now it works correctly !!

thanks a lot !!

Ouch,

s/rb_string_eval/rb_eval_string/

Sorry for noise.

···

On 2/23/06, Gyoung-Yoon Noh <nohmad@gmail.com> wrote:

On 2/23/06, hongseok.yoon@gmail.com <hongseok.yoon@gmail.com> wrote:
> I tried to call rb_eval_string(), so I wrote simple C code.
>
> #include "/home/xopht/lib/1.8/i686-linux/ruby.h"
>
> int main()
> {
> rb_eval_string( "puts" );
> return 0;
> }
>
> I compiled it and get bellow result.
>
> [xopht@odin ruby]$ g++ -o test test.cc -lruby-static -L/home/xopht/lib/
> /home/xopht/lib//libruby-static.a(string.o)(.text+0x319a): In function
> `rb_str_crypt':
> /home/xopht/ruby/ruby-1.8.4/string.c:4360: undefined reference to
> `crypt'
> /home/xopht/lib//libruby-static.a(dln.o)(.text+0x9f): In function
> `dln_load':
> /home/xopht/ruby/ruby-1.8.4/dln.c:1351: undefined reference to `dlopen'
> /home/xopht/lib//libruby-static.a(dln.o)(.text+0xb6):/home/xopht/ruby/ruby-1.8.4/dln.c:1356:
> undefined reference to `dlsym'
> /home/xopht/lib//libruby-static.a(dln.o)(.text+0x12b):/home/xopht/ruby/ruby-1.8.4/dln.c:1359:
> undefined reference to `dlclose'
> /home/xopht/lib//libruby-static.a(dln.o)(.text+0x5): In function
> `dln_strerror':
> /home/xopht/ruby/ruby-1.8.4/dln.c:1193: undefined reference to
> `dlerror'
> collect2: ld returned 1 exit status
> [xopht@odin ruby]$
>
> what's wrong?
>
>
>

rb_string_eval() needs to be initialized properly.
Check this,
Extending Ruby
http://www.rubygarden.org/ruby?EmbedRuby

--
http://nohmad.sub-port.net

--
http://nohmad.sub-port.net

i also got the same error.
bye :slight_smile:
Ashish