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