# I'm trying to integrate my C++ program with Ruby using SWIG

**URL:** <https://www.ruby-forum.com/t/im-trying-to-integrate-my-c-program-with-ruby-using-swig/70657>\
**Category:** Ruby\
**Created:** [October 1, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/im-trying-to-integrate-my-c-program-with-ruby-using-swig/70657 "2006-10-01T17:42:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bryanedds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@bryanedds](https://www.ruby-forum.com/u/bryanedds)\
**Post date:** [October 1, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/im-trying-to-integrate-my-c-program-with-ruby-using-swig/70657/1 "2006-10-01T17:42:53Z")

</div>

All I can find is some outdated docs with command line instructions  
that don’t work. Can someone please point in differenct direction?

Thanks.

---

<div class="post-metadata">

**Author:** ![bryanedds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@bryanedds](https://www.ruby-forum.com/u/bryanedds)\
**Post date:** [October 1, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/im-trying-to-integrate-my-c-program-with-ruby-using-swig/70657/2 "2006-10-01T18:27:00Z")

</div>

bryanedds wrote:

> All I can find is some outdated docs with command line instructions  
> that don’t work. Can someone please point in differenct direction?

Raw Ruby is easier than SWIG - even when you need all of SWIG’s  
features.

Here’s some of my integrational code. First, we have a super-thin  
wrapper  
for the Ruby VALUE type, the root type of all Ruby expressions:

```
class

```

rbValue  
{  
public:

rbValue(VALUE nuV = Qnil):  
v(nuV)  
{}

rbValue(char const \* gv):  
v(Qnil)  
{  
assert(gv);  
assert(’$’ == gv[0]); // documentation sez this is optional. We  
don’t  
agree  
v = rb\_gv\_get(gv);  
}

```
operator VALUE() const { return v; } // TODO deprecate me
rbValue &operator =(VALUE nuV) { v = nuV; return *this; } // 

```

TODO  
deprecate me

```
rbValue

```

fetch(char const \* tag)  
{  
return funcall(“fetch”, 2, rb\_str\_new2(tag), Qnil);  
}

```
rbValue

```

fetch(int idx)  
{  
return funcall(“fetch”, 2, INT2FIX(idx), Qnil);  
}

```
rbValue

```

fetch(size\_t idx)  
{  
return funcall(“fetch”, 2, INT2FIX(idx), Qnil);  
}

```
VALUE *

```

getPtr()  
{  
assert(T\_ARRAY == TYPE(v));  
return RARRAY(v)-\>ptr;  
}

```
long

```

getLen()  
{  
assert(T\_ARRAY == TYPE(v));  
return RARRAY(v)-\>len;  
}

```
rbValue

```

getAt(long idx)  
{  
assert(idx \< getLen());  
return RARRAY(v)-\>ptr[idx];  
}

```
rbValue

```

operator[](long idx)  
{  
return getAt(idx);  
}

```
bool isNil() { return Qnil == v; }

double to_f()
    {
    assert(T_FLOAT == TYPE(v) || T_FIXNUM == TYPE(v));
    return NUM2DBL(v);
    }

char const * to_s()
    {
    assert(T_STRING == TYPE(v));
    return STR2CSTR(v);
    }

rbValue

```

funcall (  
char const \* method,  
int argc = 0,  
VALUE arg1 = Qnil,  
VALUE arg2 = Qnil,  
VALUE arg3 = Qnil  
)  
{  
return rb\_funcall(v, rb\_intern(method), argc, arg1, arg2, arg3);  
}

```
rbValue

```

iv\_get(char const \* member)  
{  
VALUE iv = rb\_iv\_get(v, member);  
return iv;  
}

```
void

```

iv\_set(char const \* member, VALUE datum)  
{  
rb\_iv\_set(v, member, datum);  
}

```
void

```

iv\_set(char const \* member, int datum)  
{  
iv\_set(member, INT2FIX(datum));  
}

private:  
VALUE v;

}; // a smart wrapper for the Ruby VALUE type

You simply use it the way you think to use a Ruby value, and (like Ruby)  
it  
will defend the actual wrapped type at runtime. Unlike Ruby, it will  
defend  
by breaking an assertion. You might like to throw, or you might write  
strongly-typed code and never break.

Read the Ruby documentation for extending to C. Extend to C-style C++,  
and  
call a Ruby script. If it creates a global variable called $egg, you can  
retrieve this variable and use it in C++ like this:

```
egg = rbValue("$egg");

if (egg.isNil())
    return false;

out = rbValue("$out"); 

```

assert(!out.isNil());  
ax = egg.iv\_get("@rootAxioms").funcall(“first”); assert(!ax  
.isNil());

And so on. The result looks like Ruby, but with extra iv\_get() and  
funcall()  
between everything. From here, writing new C++ to drive Ruby is very  
easy,  
and converting Ruby to Ruby-style C++ is also super easy.

---

<div class="post-metadata">

**Author:** ![bryanedds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@bryanedds](https://www.ruby-forum.com/u/bryanedds)\
**Post date:** [October 1, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/im-trying-to-integrate-my-c-program-with-ruby-using-swig/70657/3 "2006-10-01T19:27:19Z")

</div>

bryanedds wrote:

> All I can find is some outdated docs with command line instructions  
> that don’t work. Can someone please point in differenct direction?
> 
> Thanks.  
> Do you have the most recent version of SWIG? I think the current  
> development version is 1.3.29. There are detailed instructions in the  
> source and on line for both the Windows interface and the Ruby  
> interface.

Older versions of SWIG don’t support C++ very well.
