Swig, Ruby, C++ question

D

Daniel Berger

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/Ruby.html, 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?
 
J

Jan Svitok

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/Ruby.html, 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.
 
D

Daniel Berger

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
 
J

Jan Svitok

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#SWIG_nn47
 

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

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top