# SWIG toy example problem

**URL:** <https://www.ruby-forum.com/t/swig-toy-example-problem/90001>\
**Category:** Ruby\
**Created:** [March 24, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/swig-toy-example-problem/90001 "2007-03-24T21:31:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Larrytheliquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/fbc32d/32.png) [@Larrytheliquid](https://www.ruby-forum.com/u/Larrytheliquid)\
**Post date:** [March 24, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/swig-toy-example-problem/90001/1 "2007-03-24T21:31:54Z")

</div>

I’m having a bit of trouble trying to use SWIG to wrap a simple C++  
header  
file, as follows:

## ‘MyTest.h’

## #define SUCCESS 1

## ‘myTest.i’

%include MyTest.h

## %module myTest %{ #include “MyTest.h” %}

swig -c++ -ruby myTest.i

g++ -c myTest\_wrap.cxx -I/usr/lib/ruby/1.8/i386-cygwin

g++ -shared myTest\_wrap.o -lruby -o myTest.so

$ irb  
irb(main):001:0\> require ‘myTest’  
=\> true  
irb(main):002:0\> MyTest::SUCCESS  
NameError: uninitialized constant MyTest::SUCCESS  
from (irb):3  
from :0  
irb(main):004:0\> MyTest.constants  
=\> []  
irb(main):005:0\>

---

<div class="post-metadata">

**Author:** ![Larrytheliquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/fbc32d/32.png) [@Larrytheliquid](https://www.ruby-forum.com/u/Larrytheliquid)\
**Post date:** [March 26, 2007, 9:46am UTC](https://www.ruby-forum.com/t/swig-toy-example-problem/90001/2 "2007-03-26T09:46:34Z")

</div>

On 3/24/07, Larrytheliquid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> %include MyTest.h
> 
> =\> []  
> irb(main):005:0\>

Hi,

somehow works for me. To check, have a look at the end of the  
generated file - there should be a line saying  
rb\_define\_const(mMyTest,“SUCCESS”, INT2NUM(1));

SWIG processor by default ignores #include (to enable them use  
–includeall) but this is not your case, as you used %include instead.

I’m using SWIG 1.3.27, as the newer ones converted ruby support to  
unified templates, and I couldn’t get our hacks to work with them yet.

Jano

---

<div class="post-metadata">

**Author:** ![Larrytheliquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/fbc32d/32.png) [@Larrytheliquid](https://www.ruby-forum.com/u/Larrytheliquid)\
**Post date:** [March 27, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/swig-toy-example-problem/90001/3 "2007-03-27T18:51:15Z")

</div>

Hi Jano,

Good call on checking for rb\_define\_const… it wasn’t there. I  
eventually  
found out that the version I was using was 1.3.29, which did not seem to  
work. After using 1.3.31 everything worked well.

Thanks.
