[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Swig, Ruby, C++ question

Daniel Berger

12/4/2006 8:40:00 PM

Hi all,

Solaris 10
swig 1.3.21
Sun Studio 11

I'm trying my hand at SWIG here. I've read over
http://www.swig.org/Doc1.3..., but I'm confused as to how to
setup the interface file so that I get a class. I don't want a
module, if possible.

Anyway, given the following .h file:

// polygon.h
class Rectangle {
int x, y;

public:
void set_values(int,int);
int area();
};

int Rectangle::area(){
return x * y;
}

void Rectangle::set_values (int a, int b) {
x = a;
y = b;
}

I created a simple interface file (polygon.i):

%module polygon

Then I ran swig like this:

swig -c++ -ruby polygon.i

That generates the file, but it only contains a Polygon module.
There's no Rectangle class or methods.

What am I doing wrong here? And, is there any way to just generate a
Rectangle class without a Polygon module?

Thanks,

Dan

PS - I tried to find the tutorial that Lyle Johnson gave back in 2002,
but I can't find a good link. Anyone have one handy?

3 Answers

Jano Svitok

12/4/2006 9:04:00 PM

0

On 12/4/06, Daniel Berger <djberg96@gmail.com> wrote:
> Hi all,
>
> Solaris 10
> swig 1.3.21
> Sun Studio 11
>
> I'm trying my hand at SWIG here. I've read over
> http://www.swig.org/Doc1.3..., but I'm confused as to how to
> setup the interface file so that I get a class. I don't want a
> module, if possible.
>
> Anyway, given the following .h file:
>
> // polygon.h
> class Rectangle {
> int x, y;
>
> public:
> void set_values(int,int);
> int area();
> };
>
> int Rectangle::area(){
> return x * y;
> }
>
> void Rectangle::set_values (int a, int b) {
> x = a;
> y = b;
> }
>
> I created a simple interface file (polygon.i):
>
> %module polygon
>
> Then I ran swig like this:
>
> swig -c++ -ruby polygon.i
>
> That generates the file, but it only contains a Polygon module.
> There's no Rectangle class or methods.
>
> What am I doing wrong here? And, is there any way to just generate a
> Rectangle class without a Polygon module?
>
> Thanks,
>
> Dan
>
> PS - I tried to find the tutorial that Lyle Johnson gave back in 2002,
> but I can't find a good link. Anyone have one handy?

You need to include the header in the interface file. In fact, you
need to include it twice: once for SWIG to parse it and create the
wrapper .c/.cxx/.cpp file, and second time in the wrapper itself to be
compilable.

/* first time for swig */
%include polygon.h

/* second time for c compiler */
%{
#include "polygon.h"
%}

you can replace these two with (in the latest version at least, so YMMV)

%inline %{
#include "polygon.h"
%}

A single #include might be enough. Try and if not, use the above form.

If the .i file is not too much complicated, you can merge it with the
h file by enclosing SWIG specifics in #ifdef SWIG, e.g.
// polygon.h
#ifdef SWIG
%module polygon
#endif

...
and running SWIG on the .h file.

Daniel Berger

12/4/2006 9:57:00 PM

0


Jan Svitok wrote:
> On 12/4/06, Daniel Berger <djberg96@gmail.com> wrote:
> > Hi all,
> >
> > Solaris 10
> > swig 1.3.21
> > Sun Studio 11
> >
> > I'm trying my hand at SWIG here. I've read over
> > http://www.swig.org/Doc1.3..., but I'm confused as to how to
> > setup the interface file so that I get a class. I don't want a
> > module, if possible.

<snip>

> You need to include the header in the interface file. In fact, you
> need to include it twice: once for SWIG to parse it and create the
> wrapper .c/.cxx/.cpp file, and second time in the wrapper itself to be
> compilable.
>
> /* first time for swig */
> %include polygon.h

This works, thanks!

> /* second time for c compiler */
> %{
> #include "polygon.h"
> %}

If this is supposed to generate a '#include "polygon.h"' entry within
the .cxx file, it doesn't seem to work. I can stuff it in there
manually easily enough, but I'd like to know what I'm doing wrong.

> you can replace these two with (in the latest version at least, so YMMV)
>
> %inline %{
> #include "polygon.h"
> %}

I had no luck with this either. It didn't cause an error, it just
didn't seem to do anything.

> A single #include might be enough. Try and if not, use the above form.
>
> If the .i file is not too much complicated, you can merge it with the
> h file by enclosing SWIG specifics in #ifdef SWIG, e.g.
> // polygon.h
> #ifdef SWIG
> %module polygon
> #endif

The .h file I ultimately want to wrap is not under my control, so this
won't be an option.

Regards,

Dan

Jano Svitok

12/4/2006 11:06:00 PM

0

On 12/4/06, Daniel Berger <djberg96@gmail.com> wrote:
>
> Jan Svitok wrote:
> > On 12/4/06, Daniel Berger <djberg96@gmail.com> wrote:
> > > Hi all,
> > >
> > > Solaris 10
> > > swig 1.3.21
> > > Sun Studio 11

> > /* second time for c compiler */
> > %{
> > #include "polygon.h"
> > %}
>
> If this is supposed to generate a '#include "polygon.h"' entry within
> the .cxx file, it doesn't seem to work. I can stuff it in there
> manually easily enough, but I'd like to know what I'm doing wrong.

Right, that's what it should do. Here are few "tips":
- - -
You're using pretty ancient version of swig (1.3.21 is a 2004 version,
the latest is 1.3.31) - it might be a bug in swig... I can run the
file on the latest swig if it would help.
- - -
From the docs[1] it seems that %module and %include should be enough. Hmmm.
- - -
%{
...
%}

should be equivalent to

%header %{
...
%}
- - -
swig -Wall (full warnings) or swig -E (preprocess only) may be a way to debug

[1] http://swig.sourceforge.net/Doc1.3/SWIG.html...