swig, c++, use string

K

killy-kun

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 <string>

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...
 
M

Mark Volkmann

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 <string>

using namespace std;

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

string bidon()
{
string res =3D "bouh";
return res;
}

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

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

#-----------------
[eurubyt:24] bidon
=3D=3D> #<SWIG::TYPE_p_string:0xb7f080e0>
[eurubyt:25] bidon.class
=3D=3D> 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...

There's an example that does this near the end of a presentation I
gave. You can get a PDF of the presentation at
http://groups.yahoo.com/group/stlruby/files/. Look for the file
RubyTools.pdf.
 
K

killy-kun

Mark said:
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 <string>

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...


There's an example that does this near the end of a presentation I
gave. You can get a PDF of the presentation at
http://groups.yahoo.com/group/stlruby/files/. Look for the file
RubyTools.pdf.

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
 
M

Mark Volkmann

Mark said:
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 <string>

using namespace std;

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

string bidon()
{
string res =3D "bouh";
return res;
}

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

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

#-----------------
[eurubyt:24] bidon
=3D=3D> #<SWIG::TYPE_p_string:0xb7f080e0>
[eurubyt:25] bidon.class
=3D=3D> 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 clas= s...

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
http://groups.yahoo.com/group/stlruby/files/. Look for the file
RubyTools.pdf.

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

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.
 
K

killy-kun

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.

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 <string>

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-promptLoadError: ./Str.so: undefined symbol: Init_Str - ./Str.so
from ./Str.so
from (irb):1=> 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" ...
 
K

killy-kun

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=> #<SWIG::TYPE_p_string:0xb7efa238>
#--------------------#

Best regards,

Guillaume
 
M

Mark Volkmann

------=_Part_9701_28204120.1142915432182
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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"

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=3D> #<SWIG::TYPE_p_string:0xb7efa238>
#--------------------#

Best regards,

Guillaume


--
R. Mark Volkmann
Object Computing, Inc.

------=_Part_9701_28204120.1142915432182
Content-Type: application/x-gzip; name="bidon.tar.gz"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="bidon.tar.gz"
X-Attachment-Id: f_el1qbkfy

H4sICEKBH0QAA2JpZG9uLnRhcgDtl9tum0AQQP28XzEiddXG4n6LkqYPUV/y6h+IYCH2SoRFLKu4
ivrvXRaMSeLGUSVsRZkj2cDMsLN4mItTlvHSnk2Ko4jDUB8VL4/63HUCJ44dJ4iV3HWiIJpBOO22
OqRokhpgVnPevGV3SP9BSXX89be1nshHG+AoCP4Zf9cLu/hHcRx7noq/14rAmWg/z/jk8T9jJS1k
lsMP0dSsXP0kRDTZ5WV3BfrF+Pb96rn0njY3WkF5KRoYqb6CUManfijk3fT5L1mRWWKiAnCo/vuu
N+R/5Edt/vuei/l/DMQjW4FJFwswa5n+hnOLkbPlza/b5bUtRW0XLLVbhe1aFzbzLyKzYKXc2GRs
w2lSvLCMdpYrSpUHMO8rRtX6tKrg/O6xTiqLbjZg3lrq80Uv15mKdVLnWW/fdSa+vYODWahyo/fL
Yanc6UJkCY5F578Y938VmUl8HOr/vuvu+n8rd70gxPw/CkP/N/oZ0CBEirbJl8lDLqqE5m1/1wPA
/t6/bfvwREDRm9W5uNLX6gSuQcACDBBJ0XQZbWyVjazLzvjP4KIfOl4tqNYxUi7Xe28+9Q/5QRnn
P5vIx8H539/9/3OiWM//Toz5fwzmDzyTRb5rpYTMn8ieqjBX+TkfxKok3HWJaTFjJB/MT/1cCIIg
CIIgCIIgCIIgCIIgCPKZ+Qs2b6WTACgAAA==
------=_Part_9701_28204120.1142915432182--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top