# Swig, Ruby, C++ question

**URL:** <https://www.ruby-forum.com/t/swig-ruby-c-question/78307>\
**Category:** Ruby\
**Created:** [December 4, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/swig-ruby-c-question/78307 "2006-12-04T21:41:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 4, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/swig-ruby-c-question/78307/1 "2006-12-04T21:41:08Z")

</div>

Hi all,

Solaris 10  
swig 1.3.21  
Sun Studio 11

I’m trying my hand at SWIG here. I’ve read over  
[SWIG and Ruby](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 J. gave back in 2002,  
but I can’t find a good link. Anyone have one handy?

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 4, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/swig-ruby-c-question/78307/2 "2006-12-04T22:05:11Z")

</div>

On 12/4/06, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rectangle class without a Polygon module?
> 
> Thanks,
> 
> Dan
> 
> PS - I tried to find the tutorial that Lyle J. 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.

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 4, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/swig-ruby-c-question/78307/3 "2006-12-04T23:01:25Z")

</div>

Jan S. wrote:

> > module, if possible.

> 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

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 5, 2006, 12:07am UTC](https://www.ruby-forum.com/t/swig-ruby-c-question/78307/4 "2006-12-05T00:07:36Z")

</div>

On 12/4/06, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Jan S. wrote:
> 
> > On 12/4/06, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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] [SWIG Basics](http://swig.sourceforge.net/Doc1.3/SWIG.html#SWIG_nn47)
