# Perl calling C calling Ruby (linking problem)

**URL:** <https://rubytalk.org/t/perl-calling-c-calling-ruby-linking-problem/1174>\
**Category:** ruby-talk\
**Created:** [13 August 2002 17:20 UTC](https://rubytalk.org/t/perl-calling-c-calling-ruby-linking-problem/1174 "2002-08-13T17:20:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Munro1](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@Peter\_Munro1](https://rubytalk.org/u/Peter_Munro1)\
**Post date:** [13 August 2002 17:20 UTC](https://rubytalk.org/t/perl-calling-c-calling-ruby-linking-problem/1174/1 "2002-08-13T17:20:58Z")

</div>

Guy,

Wow! What can I say? Many thanks for looking at this problem. And so  
quickly. I’m in awe.

I’ll take a long look at my setup. I didn’t have the ruby init stuff you  
put in your ruby.i, but I’d put that in my embedded C code. I’ll  
experiment with this.

Many thanks again for your help. I’m really appreciative.

Best regards,  
Peter Munro

> > I’m running:  
> > Ruby 1.67  
> > Perl 5.6.1  
> > swig 1.3
> 
> Well there is a problem
> 
> pigeon% ls  
> a.pl\* regdb.i ruby.i  
> pigeon%
> 
> pigeon% cat regdb.i  
> %module regdb  
> %{  
> #include “ruby.h”  
> %}  
> %include ruby.i  
> pigeon%
> 
> pigeon% cat ruby.i  
> %{  
> static int initi = 0;  
> %}
> 
> %inline %{
> 
> int init(void) {  
> if (!initi) {  
> ruby\_init();  
> ruby\_init\_loadpath();  
> initi = 1;  
> }  
> return 0;  
> }
> 
> char \*runruby(char \*c) {  
> VALUE res;  
> int error = 0;
> 
> if (!initi) {  
> init();  
> }  
> res = rb\_eval\_string\_protect(c, &error);  
> if (error) {  
> res = rb\_gv\_get(“$!”);  
> }  
> res = rb\_obj\_as\_string(res);  
> return RSTRING(res)-\>ptr;  
> }
> 
> %}
> 
> pigeon%
> 
> swig was compiled with :
> 
> ./configure --prefix=/home/ts/local/sw --enable-perl5  
> –with-ruby=/home/ts/local/r167/bin/ruby \>  
> pigeon% ~/local/sw/bin/swig -version
> 
> SWIG Version 1.3.14u-20020813-1606  
> Copyright (c) 1995-1998  
> University of Utah and the Regents of the University of California  
> Copyright (c) 1998-2001  
> University of Chicago
> 
> Compiled with CC  
> pigeon%
> 
> pigeon% perl -v
> 
> This is perl, v5.6.1 built for i386-linux
> 
> Copyright 1987-2001, Larry Wall
> 
> Perl may be copied only under the terms of either the Artistic License  
> or the \>GNU General Public License, which may be found in the Perl 5  
> source kit. \>  
> Complete documentation for Perl, including FAQ lists, should be found on  
> this system using `man perl' or `perldoc perl’. If you have access to the  
> Internet, point your browser at [http://www.perl.com/](http://www.perl.com/), the Perl Home Page.
> 
> pigeon%
> 
> pigeon% ~/local/r167/bin/ruby -v  
> ruby 1.6.7 (2002-03-01) [i686-linux]  
> pigeon%
> 
> pigeon% ~/local/sw/bin/swig -perl regdb.i  
> pigeon%
> 
> pigeon% gcc -c regdb\_wrap.c -I/usr/lib/perl/5.6.1/CORE -Dbool=char  
> -I/home/ts/local/r167/lib/ruby/1.6/i686-linux \>In file included from  
> /home/ts/local/r167/lib/ruby/1.6/i686-linux/ruby.h:610, \>from  
> regdb\_wrap.c:515:  
> /home/ts/local/r167/lib/ruby/1.6/i686-linux/intern.h:258: warning:  
> `yyparse' redefined >/usr/lib/perl/5.6.1/CORE/embed.h:756: warning: this is the location of the previous definition /home/ts/local/r167/lib/ruby/1.6/i686-linux/intern.h:259: warning: `yylex’ redefined \>/usr/lib/perl/5.6.1/CORE/embed.h:755: warning: this is  
> the location of the previous definition  
> /home/ts/local/r167/lib/ruby/1.6/i686-linux/intern.h:260: warning:  
> `yyerror’ redefined \>/usr/lib/perl/5.6.1/CORE/embed.h:751: warning: this  
> is the location of the previous definition \>pigeon%
> 
> pigeon% ld -shared regdb\_wrap.o -L/home/ts/local/r167/lib -lruby -ldl  
> -lm -lcrypt -o regdb.so \>pigeon%

> **···**
>
> > pigeon% cat a.pl  
> > #!/usr/bin/perl -l  
> > use regdb;  
> > $res = regdb::runruby("  
> > require ‘digest/md5’  
> > Digest::MD5.new(‘hello’)  
> > ");  
> > print $res;  
> > pigeon%
> > 
> > pigeon% a.pl  
> > 5d41402abc4b2a76b9719d911017c592  
> > pigeon%
> > 
> > Guy Decoux
