# Swig, c++, use string

**URL:** <https://www.ruby-forum.com/t/swig-c-use-string/84155>\
**Category:** Ruby\
**Created:** [March 20, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/swig-c-use-string/84155 "2006-03-20T14:43:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![killy-kun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@killy-kun](https://www.ruby-forum.com/u/killy-kun)\
**Post date:** [March 20, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/swig-c-use-string/84155/1 "2006-03-20T14:43:56Z")

</div>

I would like to use the c++ string type as a parameter of function  
return of some c++ functions used in a ruby module, made with swig.

Here is a simple example I tried to use the string type :

#-----------------  
%module str

%inline %{

// pour tester les string

#include

using namespace std;

string fctBidon (string s)  
{  
string res;  
res = s + " salt bidon";  
return res;  
}

string bidon()  
{  
string res = “bouh”;  
return res;  
}

%}  
#-----------------

But when I use it, here are the errors I get :

#-----------------  
[eurubyt:24] bidon  
==\> #SWIG::TYPE\_p\_string:0xb7f080e0  
[eurubyt:25] bidon.class  
==\> SWIG::TYPE\_p\_string  
[eurubyt:26] fctBidon(“bb”)  
TypeError: in method ‘fctBidon’, argument 1 of type ‘string’  
from (irb):26:in `fctBidon’  
from (irb):26  
#-----------------

That means the c++ string type isn’t considered as the ruby String  
class…

Anybody knows how I can make this work ? I would like the c++ string  
type to be considered as the ruby String class…

---

<div class="post-metadata">

**Author:** ![killy-kun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@killy-kun](https://www.ruby-forum.com/u/killy-kun)\
**Post date:** [March 20, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/swig-c-use-string/84155/2 "2006-03-20T15:20:21Z")

</div>

On 3/20/06, killy-kun [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> // pour tester les string  
> }  
> But when I use it, here are the errors I get :  
> #-----------------
> 
> That means the c++ string type isn’t considered as the ruby String class…
> 
> Anybody knows how I can make this work ? I would like the c++ string  
> type to be considered as the ruby String class…

There’s an example that does this near the end of a presentation I  
gave. You can get a PDF of the presentation at  
[Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](http://groups.yahoo.com/group/stlruby/files/). Look for the file  
RubyTools.pdf.

---

<div class="post-metadata">

**Author:** ![killy-kun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@killy-kun](https://www.ruby-forum.com/u/killy-kun)\
**Post date:** [March 20, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/swig-c-use-string/84155/3 "2006-03-20T19:34:01Z")

</div>

Mark V. wrote:

> > %inline %{  
> > res = s + " salt bidon";  
> > #-----------------  
> > from (irb):26:in `fctBidon’  
> > gave. You can get a PDF of the presentation at  
> > [Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](http://groups.yahoo.com/group/stlruby/files/). Look for the file  
> > RubyTools.pdf.
> 
> –  
> R. Mark V.  
> Object Computing, Inc.

Thank you very much.  
But the problem doesn’t seem tobe resolved 😕  
The only thing I had to add seems to be :  
%include “std\_string.i”

I did so, the compilation goes well, and seems to take the file into  
account.  
But the effect is still exactely the same…

I tried your example, but had the following error when running the  
main.rb file :

ruby: symbol lookup error: ./RubyPerson.so: undefined symbol:  
\_ZN6PersonC1ERKSs

---

<div class="post-metadata">

**Author:** ![killy-kun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@killy-kun](https://www.ruby-forum.com/u/killy-kun)\
**Post date:** [March 20, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/swig-c-use-string/84155/4 "2006-03-20T22:29:24Z")

</div>

> I’m not sure what else to suggest. If you’d like, tar up your code and  
> your build script, email it to me, and I’ll try to get it to run.
> 
> –  
> R. Mark V.  
> Object Computing, Inc.

Thank you very much for your proposition.  
For the moment, the only thing I trying to make work is the str.i, where  
all the code is included, and that is displayed below.  
Here is what I tried :

#------------------------------#  
[gnargeot@dptinfo ex3]$ cat str.i  
%module str

%inline %{  
#include

using namespace std;

string fctBidon (const string& s)  
{  
string res;  
res = s + " salt bidon";  
return res;  
}

string bidon()  
{  
string res = “bouh”;  
return res;  
}

%}

%include “std\_string.i”  
[gnargeot@dptinfo ex3]$ swig -c++ -ruby \*.i  
[gnargeot@dptinfo ex3]$ set RBDIR = /usr/lib/ruby/1.8/i386-linux/  
[gnargeot@dptinfo ex3]$ gcc -c -fpic \*\_wrap.cxx -I. -I$RBDIR  
[gnargeot@dptinfo ex3]$ gcc -shared -fpic \*\_wrap.o -lstdc++ -o Str.so  
[gnargeot@dptinfo ex3]$ irb --simple-prompt

> > require ‘./Str.so’  
> > LoadError: ./Str.so: undefined symbol: Init\_Str - ./Str.so  
> > from ./Str.so  
> > from (irb):1
> 
> > require ‘./Str.so’  
> > =\> false  
> > #------------------------------#

Hope I will manage to make this work 😕  
For the moment, it doesn’t work at all, compared to my previous building  
method, that used the Makefile provided in the examples directories of  
the swig pack. But when building with these makefiles, I still had c++  
strings recognized as “TYPE\_p\_string” …

---

<div class="post-metadata">

**Author:** ![killy-kun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@killy-kun](https://www.ruby-forum.com/u/killy-kun)\
**Post date:** [March 20, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/swig-c-use-string/84155/5 "2006-03-20T20:23:27Z")

</div>

On 3/20/06, killy-kun [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ```
> > > string res;
> > > 
> > > ```
> > > 
> > > %}  
> > > TypeError: in method ‘fctBidon’, argument 1 of type ‘string’  
> > > There’s an example that does this near the end of a presentation I  
> > > Thank you very much.
> 
> ruby: symbol lookup error: ./RubyPerson.so: undefined symbol:  
> \_ZN6PersonC1ERKSs

I’m not sure what else to suggest. If you’d like, tar up your code and  
your build script, email it to me, and I’ll try to get it to run.

---

<div class="post-metadata">

**Author:** ![killy-kun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@killy-kun](https://www.ruby-forum.com/u/killy-kun)\
**Post date:** [March 20, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/swig-c-use-string/84155/6 "2006-03-20T22:39:28Z")

</div>

Sorry, I made an error last time, I corrected this :  
replaced “%module Str” by “%module Str” into the str.i file.

Now the require instruction works fine, but the type “error” is still  
the same :

#--------------------#  
[gnargeot@dptinfo ex3]$ swig -c++ -ruby \*.i  
[gnargeot@dptinfo ex3]$ gcc -c -fpic \*\_wrap.cxx -I. -I$RBDIR  
[gnargeot@dptinfo ex3]$ gcc -shared -fpic \*\_wrap.o -lstdc++ -o Str.so  
[gnargeot@dptinfo ex3]$ irb --simple-prompt

> > require ‘Str.so’  
> > =\> true
> 
> > include Str  
> > =\> Object
> 
> > bidon  
> > =\> #SWIG::TYPE\_p\_string:0xb7efa238  
> > #--------------------#

Best regards,

Guillaume

---

<div class="post-metadata">

**Author:** ![killy-kun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@killy-kun](https://www.ruby-forum.com/u/killy-kun)\
**Post date:** [March 21, 2006, 5:30am UTC](https://www.ruby-forum.com/t/swig-c-use-string/84155/7 "2006-03-21T05:30:58Z")

</div>

I’ve attached a zip that includes working code.  
To build it, enter “build.sh”. You may need to change the definition  
of RBDIR in this file.  
To use it from irb, enter these statements.

require ‘RubyBidon.so’  
include RubyBidon  
bidon  
fctBidon(‘foo’)

This outputs  
“bouh”  
“foo salt bidon”
